0

如何在 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
4

1 回答 1

1

ActiveRecord 有一个名为after_initialize的方法,它是一个类似于 after_load 的回调。

于 2013-01-17T17:46:35.860 回答