我遇到了依赖销毁功能的一些问题。 模型如下。
Company.rb
has_many :company_users, dependent: :destroy
has_many :users, through: :company_users, dependent: :destroy
CompanyUser.rb
belongs_to :company
belongs_to :user
User.rb
has_one :company_user, dependent: :destroy
has_one :company, through: :company_user
当我删除公司时,它需要删除公司用户以及用户。
目前只删除公司用户。关于如何解决这个问题的任何想法?
答案 0 :(得分:3)
这将有效,并在内容中找到已完成的更改
class Company < ActiveRecord::Base
has_many :users, through: :company_users #removed dependent destroy
has_many :company_users, dependent: :destroy
end
class User < ActiveRecord::Base
has_one :company_user
has_one :company, through: :company_user
end
class CompanyUser < ActiveRecord::Base
belongs_to :company
belongs_to :user, dependent: :destroy #added dependent destroy
end
日志
DELETE FROM "company_users" WHERE "company_users"."id" = ?[0m [["id", 4]]
DELETE FROM "users" WHERE "users"."id" = ?[0m [["id", 4]]
DELETE FROM "companies" WHERE "companies"."id" = ? [["id", 4]]