ruby-on-rails – Rails:dependent => destroy,想要调用另一个动作而不是destroy

ruby-on-rails – Rails:dependent => destroy,想要调用另一个动作而不是destroy,第1张

概述我有一个完美运行的has_many:through模型. has_many :varietals has_many :grapes, :through => :varietals, :dependent => :destroy 我想调用另一个动作而不是:destroy.事实上,我不想取消项目或销毁它,我想将记录状态字段从1更新为0而不是销毁记录. 如何调用自定义方法而不是销毁?我想我可以在模型 我有一个完美运行的has_many:through模型.

has_many :varIEtals  has_many :grapes,:through => :varIEtals,:dependent => :destroy

我想调用另一个动作而不是:destroy.事实上,我不想取消项目或销毁它,我想将记录状态字段从1更新为0而不是销毁记录.

如何调用自定义方法而不是销毁?我想我可以在模型中做到这一点……谢谢.

这个方法放在哪里?在主模型或模型中,记录将被销毁?

编辑:

我很抱歉,但我认为我没有解释我的问题.我的问题不仅仅是在主模型被销毁之后的某些东西.我想在VarIEtal模型中自定义destroy动作,即使主记录没有被破坏.

就像是:

class VarIEtal < ActiveRecord::Base    private      def destroy        self.update_attributes(:status => 0)      endend

实际上这个动作没有被称为……

解决方法 has_many:dependent仅限于几个选项.根据 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,想要调用另一个动作而不是destroy全部内容,希望文章能够帮你解决ruby-on-rails – Rails:dependent => destroy,想要调用另一个动作而不是destroy所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1250186.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存