我有一个User
模型,其中has_many :scores
。has_many上的更新应更新父类上的属性
如果我添加一个:分给用户,用户应该做的playcounter的重新计算:
class User < ActiveRecord::Base
has_many :scores, inverse_of: :user
accepts_nested_attributes_for :scores
attr_accessible :level
before_save: set_levels
def set_levels
self.level = calculate_level
end
def calculate_level
self.scores.count
end
end
当我通过User.score.create(:time => 10)
加分分数获取的保存,但水平没有更新。
如果孩子被绑住,我怎么能重新运行set_levels
?
(它不是一个countercache列,方法也更复杂,只是在这里勾勒)
千恩万谢
嗨安德鲁的工作,感谢您的回复。我有另一种方法解决了我的问题。 – Jan
如果您直接创建乐谱 - 即Score.create(user_id:1,value:123),我认为您的解决方案不会奏效,因为它不会更新父项,并触发用户级别更新。 –