我在我的country.rb
中有一個方法,我定義了一個新的方法。保存在模型中
class Country < ActiveRecord::Base
has_many :states, :dependent => :destroy
after_save :count_total_states
def count_total_states
self.duration = State.count(:conditions => { :country_id => self.id })
Country.save!(:duration => self.duration)
end
end
我得到了self.duration
結果我想要的。但是,當我運行它時,它說
undefined method 'save!' for #<Class:0x111170d10>
我希望它計數狀態的數量屬於國家每次一個新的狀態被創建。請告訴我該怎麼做。謝謝。
爲什麼我需要做'after_save'的原因是因爲我想在確定新狀態的時候保存數據,然後再進行計數。如果我在保存之前進行計數,則不包括新狀態。 – Victor 2012-01-04 15:58:20
好。我更新了我的例子。 – 2012-01-04 16:00:07
它仍然導致無限循環... – Victor 2012-01-04 16:05:01