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