ruby-on-rails - Rails :dependent => destroy,想要调用另一个 Action 而不是破坏

标签 ruby-on-rails destroy

我有一个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/

相关文章:

mysql - Rails 2.3.2 和 MySQL 5.5.x

jquery - 如何向 Rails 3.1 添加 jquery 插件

ruby-on-rails - 销毁和删除之间的区别

Python 方法奇怪地暂停,直到 tkinter root 关闭

migration - 删除了migration.rb文件? Rails 中的潜在错误?

ruby-on-rails - rails, activerecord, 获取当前连接规范

css - 具有来自 View 的动态 Assets url 背景图像的 Rails

ruby-on-rails - 在 Heroku 区域设置弃用警告上部署 Rails 4.0.2

android - 当设备旋转时调用 onDestroy 时线程会发生什么

c - C中读写锁的线程安全销毁