【发布时间】:2012-05-04 18:05:51
【问题描述】:
我正在开发一个 Rails 应用程序,用户可以在其中创建项目。有两种类型的用户Admins 和Collaborators。 Admins 和 Collaborators has_many :accounts, through: :account_users,其中 account_users 是一个连接表。当管理员删除他们的帐户时,我也想删除他们创建的帐户和它的项目,但我无法让它工作。我的模型目前看起来像这样:
class Collaborator < User
[...]
has_many :account_users
has_many :accounts, through: :account_users
[...]
end
class Admin < User
has_many :account_users
has_many :accounts, through: :account_users, :dependent => :destroy
[...]
end
class Account < ActiveRecord::Base
[...]
belongs_to :admin
has_many :account_users
has_many :collaborators, through: :account_users
[...]
end
class AccountUser < ActiveRecord::Base
belongs_to :admin
belongs_to :account
belongs_to :collaborator
end
当管理员用户删除其帐户时,仅删除联接表和用户表中的行,不会删除他们的项目。
注意,我使用 devise 来处理身份验证。
我该如何解决这个问题?
【问题讨论】:
-
你有一个混合哈希语法:
through: :account_users, :dependent => :destroy...最好选择一个或另一个
标签: ruby-on-rails ruby database activerecord