我有一个通过 has_many 关联到项目模型的任务模型,需要在通过关联删除/插入之前操作数据。
由于“Automatic deletion of join models is direct, no destroy callbacks are triggered. ”,我不能为此使用回调。
在任务中,我需要所有的 project_ids 来计算任务保存后的项目值。
如何通过关联禁用删除或更改删除以在 has_many 上销毁?
这个问题的最佳实践是什么?
class Task
has_many :project_tasks
has_many :projects, :through => :project_tasks
class ProjectTask
belongs_to :project
belongs_to :task
class Project
has_many :project_tasks
has_many :tasks, :through => :project_tasks
最佳答案
似乎我必须使用 associations callbacks before_add
, after_add
, before_remove
或 after_remove
class Task
has_many :project_tasks
has_many :projects, :through => :project_tasks,
:before_remove => :my_before_remove,
:after_remove => :my_after_remove
protected
def my_before_remove(obj)
...
end
def my_after_remove(obj)
...
end
end
关于ruby-on-rails - 如何通过关联在 has_many 中使用回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10597878/