Ruby on rails 在:dependent=>;方面遇到问题:销毁和实例变量
我有一个项目与许多项目;它是Ruby on rails 在:dependent=>;方面遇到问题:销毁和实例变量,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个项目与许多项目;它是:dependent=>:destroy。 我试图告诉rails在调用回调(特别是Item的after\u destroy)时,仅当该项“单独”销毁时才运行,但所有项目都没有被销毁。 当整个项目被销毁时,我实际上根本不需要在销毁后运行这个方法 我不想执行:dependent=>:delete,因为该项有许多其他关联连接到它(使用:dependent=>:destroy) 它只适用于类变量,但我希望它适用于实例变量: class Project < ActiveR
:dependent=>:destroy
。
我试图告诉rails在调用回调(特别是Item的after\u destroy
)时,仅当该项“单独”销毁时才运行,但所有项目都没有被销毁。
当整个项目被销毁时,我实际上根本不需要在销毁后运行这个方法
我不想执行:dependent=>:delete
,因为该项有许多其他关联连接到它(使用:dependent=>:destroy
)
它只适用于类变量,但我希望它适用于实例变量:
class Project < ActiveRecord::Base
has_many :items, :dependent => :destroy
before_destroy :destroying_the_project
def destroying_the_project
# this is a class variable, but I wish I could had @destroying_me
# instead of @@destroying_me.
@@destroying_me = true
end
def destroying_the_project?
@@destroying_me
end
end
class Item < ActiveRecord::Base
belongs_to :project
after_destroy :update_related_statuses
def update_related_statuses
# I with I could had return if project.destroying_the_project?
# but since the callback gets the project from the DB, it's another instance,
# so the instance variable is not relevant here
return if Project::destroying_the_project?
# do a lot of stuff which is IRRELEVANT if the project is being destroyed.
# this doesn't work well since if we destroy the project,
# we may have already destroyed the suites and the entity
suite.delay.suite_update_status
entity.delay.update_last_run
end
end
class项目:销毁
销毁前:销毁项目
def销毁__项目
#这是一个类变量,但我希望我能有@destroming\u me
#而不是毁了我。
@@毁灭我=真的
结束
def摧毁了__项目?
@@毁灭我
结束
结束
类项
我能想到的另一个选项是删除:dependent=>:destroy
,并在destroy
方法之后手动处理项目中项目的销毁,但它看起来也太难看了,尤其是由于Project
有许多带有:dependent=>:destroy
的项目类型,因此必须转换为该方法
如果您在删除整个项目时不需要使用回调,您可以使用delete_all而不是destroy,任何想法都将不胜感激
我希望这不是最好的解决方案,但至少它可以工作,并且不会通过类变量引入任何全局状态:
class Project < ActiveRecord::Base
has_many :items
before_destroy :destroying_the_project
def destroying_the_project
Rails.logger.info 'Project#destroying_the_project'
items.each &:destroy_without_statuses_update
end
end
class Item < ActiveRecord::Base
belongs_to :project
after_destroy :update_related_statuses,
:unless => :destroy_without_statuses_update?
def update_related_statuses
Rails.logger.info 'Item#update_related_statuses'
end
def destroy_without_statuses_update
@destroy_without_statuses_update = true
destroy
end
def destroy_without_statuses_update?
!!@destroy_without_statuses_update
end
end
class项目:在没有\u状态\u更新的情况下销毁\u?
def更新_相关_状态
Rails.logger.info“项目#更新_相关_状态”
结束
def销毁(无状态更新)
@在没有状态的情况下销毁\u\u更新=真
破坏
结束
def销毁而不更新状态?
!!@销毁\u而不更新\u状态\u
结束
结束
类变量当然不正确,因为它适用于所有项目。你真的只想要一个项目实例…DGM-我同意。这个类变量绝对是一个坏选项。嗨,这不起作用,因为我有很多其他变量与Item.rb有很多关系。如果我将执行delete_all,它将只删除项目,而不删除项目的关系。所以,如果项目有很多:文件,:dependent=>:destroy,它不会删除文件。你确定吗?我还以为是瀑布呢。唯一的区别是回调是否运行…是的。如果您要删除\u all,它将直接从数据库中删除它,而不加载Items类,并且它不会删除Items的关联。您好,谢谢,这就是我所说的“我能想到的另一个选项是删除:dependent=>:destroy并在\u destroy方法之后手动处理项目内部的项目销毁”,所以我知道这会奏效,但我正在努力找到更好的解决方案。谢谢我也很喜欢这个@destroy\u没有状态更新(简单且有效!)@user198026,:dependent=>:destroy
的问题是,在调用父模型上的任何before destroy回调之前,关联都会被销毁。至少在Rails3.1上是这样的。这让我想知道为什么你的解决方案是有效的。我认为只有在销毁之前声明关联,关联才会在销毁之前被销毁。实际上,这就是我在上面的例子中所展示的,所以你是对的——这段代码不应该工作。但实际上在我的代码中有很多:items,:dependent=>:destroy行在before\u destroy声明之后。这就是它起作用的原因。我也同意,这还远远不够干净,而且涉及到所有的项目。这就是为什么您的解决方案更好的原因,但我还是希望找到一个更简单的解决方案。谢谢,我不知道关联声明和销毁回调的顺序很重要。你看过rails文档中提到的吗?