简体   繁体   English

无法删除Ruby on Rails中的记录

[英]Can't Delete Records In Ruby on Rails

I have three models. 我有三种模式。 Customer, Job, and Receipt. 客户,工作和收据。

Customer has many Jobs and Jobs has many Receipts. 客户有很多工作,而工作有很多收据。 Job belongs to Customer and Receipts belongs to Job. 作业属于客户,收据属于作业。

In my delete def for my customer, I want to delete all of the customers jobs and the job receipts. 在我的客户删除定义中,我要删除所有客户工作和工作收据。 Here is my controller code 这是我的控制器代码

def destroy
    customer = Customer.find(params[:id])
    customer.jobs.receipts.destroy_all #this line crashes
    customer.jobs.destroy_all
    customer.destroy
    redirect_to customers_url

    redirect_to show_todays_jobs_path
end

The line that says customer.jobs.receipts.destroy_all throws an error that states that the receipts method is undefined. 显示customer.jobs.receipts.destroy_all的行将引发错误,指出未定义收据方法。

However, in my Jobs controller, jobs.receipts.destroy_all works just fine. 但是,在我的Jobs控制器中,jobs.receipts.destroy_all可以正常工作。 In the customer controller if I remove the line to destroy receipts then that works fine too. 在客户控制器中,如果我删除行以销毁收据,那也可以正常工作。

I don't understand why it I can't delete the receipts in the Customer controller. 我不明白为什么无法在客户控制器中删除收据。 Please help me out here. 请帮我在这里。 Thanks in advance 提前致谢

Problem in your code that customer.jobs is a collection where each job record has its own collection of receipts. 您的代码中的问题是, customer.jobs是一个集合,其中每个工作记录都有自己的收据集合。 You may use association has_many :receipts, through: :jobs in the Customer model to obtain direct customer.receipts reference, then you can call customer.receipts.delete_all . 您可以在客户模型中使用has_many :receipts, through: :jobs关联has_many :receipts, through: :jobs获得直接的customer.receipts参考,然后可以调用customer.receipts.delete_all Documentation here . 文档在这里

Seems you could use dependent: :destroy or dependent: :delete_all for your associations has_many , briefly it will delete associations when the customer object is destroyed. 似乎您可以对关联has_many使用dependent: :destroydependent: :delete_all ,简而言之,当销毁customer对象时,它将删除关联。 Checkout the documentation . 查看文档

Take a look on code example: 看一下代码示例:

class Customer
  has_many :jobs, dependent: :destroy
end


class Job
  has_many :receipts, dependent: :destroy
end

Then when you call customer.destroy all related jobs and receipts could be destroyed as well. 然后,当您致电customer.destroy所有相关的工作和收据也可能被破坏。

PS. PS。 You have another mistake in controller code - redirect_to called twice, only one time is possible. 您在控制器代码中还有另一个错误redirect_to调用了两次,只能一次。

You shouldn't be doing these deletes yourself manually in your controller. 您不应该在控制器中手动执行这些删除操作。 There are parameters you can provide to your associations in your models to tell them to destroy the associated records when destroying the parent. 您可以在模型中为关联提供一些参数,以告诉它们在销毁父级时销毁关联的记录。

I've made some assumptions about your model associations, but something along the lines of the following should work for you: 我已经对您的模型关联进行了一些假设,但以下几条内容应该对您有用:

class Customer < ActiveRecord::Base
  has_many :jobs, dependent: :destroy
end

class Job < ActiveRecord::Base
  belongs_to :customer
  has_many :receipts, dependent: :destroy
end

class Receipt < ActiveRecord::Base
  belongs_to :job
end

Setting dependent: :destroy on the association tells rails to delete the associated records by calling their destroy method when deleting the parent object. 在关联上设置dependent: :destroy会告诉rails在删除父对象时通过调用它们的destroy方法来删除关联的记录。

With this set up, you can just do this in your destroy action within your controller (note there's no need to find your record before you destroy it): 通过此设置,您可以在控制器中的destroy操作中执行此操作(请注意, destroy记录之前无需find记录):

def destroy
  Customer.destroy(params[:id])
  redirect_to customers_url
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM