Rails:依赖 =>销毁,想要调用另一个操作而不是销毁

发布于 2025-01-07 02:36:09 字数 636 浏览 4 评论 0原文

我有一个运行良好的 has_many :through 模型。

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

我想调用另一个操作而不是:destroy。事实上,我不想使该项目无效或销毁它,我想将记录状态字段从 1 更新为 0,而不是销毁记录。

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

这个方法该放在哪里呢?在主模型或模型里的记录会被破坏吗?

编辑

很抱歉,但我认为我没有充分解释我的问题。我的问题不仅仅是主模型被破坏后的事情。我想在 Varietal 模型本身中自定义销毁操作,即使主记录没有被销毁。

比如:

class Varietal < ActiveRecord::Base

    private
      def destroy
        self.update_attributes(:status => 0)
      end
end

实际上这个动作不叫......

I have a has_many :through model that works perfectly.

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

I would like to call another action instead of :destroy. In fact, I don't want to nullify the item OR destroy it, I want to update the record status field from 1 to 0 instead of destroy the record.

How to call a custom method instead of destroy ? I suppose I can do that in the model itself... Thanks.

Where to put this method ? In the master model or in the model where the record will be destroyed ?

EDIT:

I'm sorry but I think I didn't enough explain my problem. My problem is not only to so something after the master model is destroyed. I want to custom the destroy action in the Varietal model itself even if the master record is not destroyed.

Something like:

class Varietal < ActiveRecord::Base

    private
      def destroy
        self.update_attributes(:status => 0)
      end
end

Actually this action is not called...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晚风撩人 2025-01-14 02:36:09

您可以使用 before_destroy 将自定义逻辑放在那里。例如,

before_destroy :reset_status

def reset_status
  ...
end

请查看此处了解更多详细信息。

You can use before_destroy to put your custom logic there. E.g.,

before_destroy :reset_status

def reset_status
  ...
end

Check here for more details.

南巷近海 2025-01-14 02:36:09

您只需要在 before_destroy 或 after_destroy 上添加回调并操作您的关联。举例来说

after_destroy :do_on_grapes

def do_on_grapes
  grapes.map(&:to_do)
end

You just need add a callback on before_destroy or after_destroy and manipulate your associations. By example

after_destroy :do_on_grapes

def do_on_grapes
  grapes.map(&:to_do)
end
长亭外,古道边 2025-01-14 02:36:09

has_many :dependent 仅限于几个选项。根据文档

:dependent 如果设置为:destroy 所有关联的对象都被销毁
通过调用它们的 destroy 方法来与该对象一起。如果设置为
:delete_all 删除所有关联对象而不调用它们
破坏方法。如果设置为:无效所有关联对象的外部
键被设置为 NULL 而不调用它们的保存回调。如果设置为
:restrict 该对象引发 ActiveRecord::DeleteRestrictionError
异常,如果有任何关联对象则无法删除。

如果与 :through 选项一起使用,则连接模型上的关联
必须是belongs_to,被删除的记录是join
记录,而不是关联的记录。

看起来您需要更改 destroy 方法来更新状态字段。

has_many :dependent is limited to only a few options. According to the 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.

It looks like you would need to alter the destroy method to update the status field.

一张白纸 2025-01-14 02:36:09

我相信解决您的问题的好方法是提供自定义销毁方法。对于此类问题有多种回答,但您应该记住 ActiveRecord 和关系,例如:

class Image < ActiveRecord::Base
  has_many :comments, dependent: :destroy

也使用触发破坏链接到您的关系的回调机制。通常您应该保留此机制并将其添加到您的自定义实现中。例如

  def destroy
    self.update deleted_at: Time.now
    run_callbacks :destroy
  end

,您也可以阅读这篇文章:
使用覆盖的 destroy-method 触发 dependent: :destroy

I believe that good approach to solve your problem is to provide a custom destroy method. There are several responses to questions like these, but you should keep in mind that ActiveRecord and Relationships like:

class Image < ActiveRecord::Base
  has_many :comments, dependent: :destroy

use callback mechanisms that trigger destroy chaining to your relations, too. Usually you should preserve this mechanism and add it to your custom implementation. E.g.

  def destroy
    self.update deleted_at: Time.now
    run_callbacks :destroy
  end

You can read this post, too:
Triggering dependent: :destroy with overridden destroy-method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文