- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在开发一个 Rails 应用程序,用户可以在其中创建项目。有两种类型的用户 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
当管理员用户删除其帐户时,只会删除连接表和用户表中的行,不会删除他们的项目。
请注意,我使用设计来处理身份验证。
我该如何解决这个问题?
最佳答案
我没有看到项目关联,所以我认为您可以通过以下两种方式之一进行:
class Account < ActiveRecord::Base
after_save :destroy_projects
private
def destroy_projects
self.projects.delete_all if self.destroyed?
end
end
或
class Account < ActiveRecord::Base
[...]
belongs_to :admin
has_many :account_users
has_many :collaborators, through: :account_users
has_many :projects, :dependent => :destroy
[...]
end
关于ruby-on-rails - 通过与 :dependent => :destroy Rails 3. 2 的关系删除 has_many 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10453919/
我有两个类(class): parent 和 child child : belongs_to :parent 和 家长 has_many :children, :dependent => :dest
我有这个模型 class XmlImport ["text/xml"] end :event_import_records 条目正在被销毁。但是 :events 不是。 has_many throu
我定义了以下简单的管道: image: name: hashicorp/terraform:light entrypoint: - '/usr/bin/env' - 'PATH
我定义了以下简单的管道: image: name: hashicorp/terraform:light entrypoint: - '/usr/bin/env' - 'PATH
在我们的应用程序中,我们覆盖了 ActiveRecord destroy 方法,以便我们的记录不会被删除(因此用户可以取消删除)。像这样: def destroy self.is_deleted
我有一个笔记模型,具有以下关联 注意.rb has_many :note_categories, :dependent => :destroy has_many :categories, :throu
我是一名 Python 初学者。尝试制作一个新按钮来关闭窗口。我收到错误消息: Exception in Tkinter callback Traceback (most recent call la
我在我的应用程序中添加了评论功能,到目前为止一切正常,直到出现此错误。我所做的是添加了删除评论功能。当我启动服务器时一切正常,但在我单击“删除”按钮后问题出现了。 错误信息 undefined loc
Rails,def destroy,没有响应 destroy.js.erb 这是我的方法: # DELETE /Groups/1 # DELETE /Groups/1.xml def de
假设我有一个 MyClass 对象的集合 MyCollection。 Set MyCollection = Nothing 是否调用每个包含的对象的析构函数,或者我应该单独设置每个对象 = Nothi
我正在尝试使用 Node.js、Express 和 Mongodb 构建 REST API。我正在使用 mongodb npm 包连接到数据库,下面是我的 sever.js 文件代码 var expr
我有一个小问题,我有以下两个模型: class CriticalProcess :destroy has_many :roles, :through => :authorizations a
我正在使用 ng2-toastr 并收到以下错误 https://www.npmjs.com/package/ng2-toastr Attempt to use a destroyed view: d
基本信息: 系统: # cat /proc/version Linux version 3.10.0-514.2.2.el7.x86_64 (builder@kbuilder.dev.centos.o
一 点睛 destroy 用于销毁 ThreadGroup,该方法只是针对一个没有任何 active 线程的 group 进行一次 destroy 标记,调用该方法的直接结果是在父 group 中将自
我开始注意到 Scope::Guard 的一些奇怪之处。 . 如果我取消定义 $guard变量作为 sub 中的最后一个语句,守卫的 sub 得到 比我预期的要晚打电话。 如果我不取消它,或者如果我做
我正在构建一个提供打开和关闭它的服务的模式。该模式有一个控制关闭按钮的小 Controller ,以及进入模式内容的模板的 $compile。 那个模板就是一个组件,当然,那个组件有一个 Contro
一直在文件馆和网上寻找答案,但没有真正找到答案,只是零零碎碎。似乎有很多建议的帖子,但没有一个有答案。 我有一个使用范围的复杂指令:true。它实际上是我正在尝试为其编写清理代码的 ng-grid 最
给定以下 Delphi 代码,Foo 在 FormClose 上是 Free,但 TFoo.Destroy 是没有被调用 - 因此 Bar 没有被 Free'd,导致内存泄漏? 我是否在这里错过了一些
https://docs.angularjs.org/guide/directive By listening to this event, you can remove event listener
我是一名优秀的程序员,十分优秀!