如何在Rails 3.x中更新关于加载的关联数据?
当我加载股票代码时,如果它们已过时,我想更新其报价。由于ActiveRecord没有像after_load这样的回调,这样做的正确方法是什么?
我的伪代码看起来像
class Stock < ActiveRecord::Base
has_many :quotes, :limit => 15, :order => 'date desc'
attr_accessible :symbol
after_load :update_history
#correct code is ( Thanks Dutow)
after_initialize :update_history
def update_history
if (quotes.count > 0)
today = Time.now
get_and_store_history unless (quotes[0].updated_at >= today.beginning_of_day && quotes[0].updated_at <= today.end_of_day)
end
end
def get_and_store_history
#Update quotes
end
end