我有一个has_many:through模型可以完美地工作。
has_many :varietals
has_many :grapes, :through => :varietals, :dependent => :destroy
我想调用另一个 Action 而不是:destroy。实际上,我不想使该项无效或销毁它,我想将记录状态字段从1更新为0,而不是销毁记录。
如何调用自定义方法而不是destroy?我想我可以在模型本身中做到这一点...谢谢。
哪里放这种方法?在主模型中还是在模型中记录将被销毁?
编辑:
对不起,但我想我还不足以解释我的问题。我的问题不仅是在破坏了主模型之后发生了什么。即使主记录未销毁,我也想在Varietal模型本身中自定义销毁 Action 。
就像是:
class Varietal < ActiveRecord::Base
private
def destroy
self.update_attributes(:status => 0)
end
end
其实这个 Action 不叫...
最佳答案
has_many:depend仅限于少数几个选项。根据documentation:
:dependent If set to :destroy all the associated objects are destroyed alongside this object by calling their destroy method. If set to :delete_all all associated objects are deleted without calling their destroy method. If set to :nullify all associated objects’ foreign keys are set to NULL without calling their save callbacks. If set to :restrict this object raises an ActiveRecord::DeleteRestrictionError exception and cannot be deleted if it has any associated objects.
If using with the :through option, the association on the join model must be a belongs_to, and the records which get deleted are the join records, rather than the associated records.
看来您需要更改destroy方法以更新状态字段。
关于ruby-on-rails - Rails :dependent => destroy,想要调用另一个 Action 而不是破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9326706/