我在我的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