【发布时间】:2011-04-27 10:52:10
【问题描述】:
我有一个Invoice 模型,它是has_many :line_items。
两个模型都有before_validation 回调。发票的回调要求首先运行行项目的回调。但是,默认情况下会运行发票的回调,然后运行每个行项目的回调。
有没有一种好的方法可以确保先验证行项目,然后验证发票?
目前,我正在玩这样的事情:
class Invoice < ActiveRecord::Base
before_validation :do_something
...
private
def do_something
line_items.each { |line_item| line_item.run_callbacks(:validation) }
# Then do whatever I need here - I've forced the callback order
end
end
有没有更好的方法来处理这个问题?
【问题讨论】:
标签: ruby-on-rails-3 activerecord callback activemodel