我有一个Rails应用程序,它使用了很多回调..所以我有很多函数被调用:after_create和:after_commit在多个模型中。
我想知道我现在这样做的方式是最好的。
基本上我有以下情况:
Class Parent < ActiveRecord::Base
has_many :children
after_create :first_function
after_commit :last_function
def first_function
if !self.processed?
self.children.create(:name => "Richard The Lion Heart")
self.processed = true
self.save!
end
end
def last_function
if self.processed?
if !self.processing?
self.process
self.save!
self.processing = true
self.save!
end
end
end
end
所以你可以看到整个事情取决于一些奇怪的双布尔检查,因为否则每次更新模型时都会调用second_function,它可以由函数本身更新,因此函数会被重复调用。
总的来说,它引出了我必须为每个要触发的回调引入新布尔检查的情况。它有效,但我不认为它优雅。我错过了什么?
答案 0 :(得分:6)
您应该能够重写该代码 - 这样的事情?当然,您的真实代码可能还有一些额外的复杂性 - 还有:此代码未经测试。
Class Parent < ActiveRecord::Base
has_many :children
# only called when a new record is created
after_create :first_function
# only called for updates, not new records, should still be inside the current transaction
after_update :last_function
private
def first_function
self.children.create(:name => "Richard The Lion Heart")
# don't call save in here, already in a transaction
end
def last_function
self.process
# don't call save in here, already in a transaction
end
def process
# doing stuff ....
self.children[0].update_attribute(:name, "Beowulf")
end
end
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
这总共有十二个回调,它为您提供了巨大的力量,可以为Active Record生命周期中的每个状态做出反应和准备。为现有记录调用Base#save的顺序类似,只是每个_create回调都被相应的_update回调替换。
使用
p = Parent.new(:foo => "bar")
p.save
p.children[0].name
# => "Richard The Lion Heart"
p.update_attributes(:baz => "fud")
p.children[0].name
# => Beowulf
来自rails控制台的ActiveRecord回调(使用awesome_print ap)
> ap ActiveRecord::Callbacks::CALLBACKS
[
[ 0] :after_initialize,
[ 1] :after_find,
[ 2] :after_touch,
[ 3] :before_validation,
[ 4] :after_validation,
[ 5] :before_save,
[ 6] :around_save,
[ 7] :after_save,
[ 8] :before_create,
[ 9] :around_create,
[10] :after_create,
[11] :before_update,
[12] :around_update,
[13] :after_update,
[14] :before_destroy,
[15] :around_destroy,
[16] :after_destroy,
[17] :after_commit,
[18] :after_rollback
]