【发布时间】:2013-03-31 17:24:45
【问题描述】:
不知道为什么会这样:
class Foo < ActiveRecord::Base
belongs_to :belongable, :polymorphic => true
def after_save
if belongable.kind_of?(User)
send(:some_method)
end
end
end
class Bar < Foo
def some_method
#Do something
end
end
class Group < ActiveRecord::Base
has_many :belongings, :as => :belongable
end
class User < ActiveRecord::Base
has_many :belongings, :as => :belongable
end
“Bar”类是从 Foo 继承的 STI 模型(Foo 具有“类型”属性)。组和用户都可以有很多条。
以下按预期工作(some_method 没有被调用):
g = Group.create
g.belongings << Bar.new
g.save
以下调用some_method:
Group.first.belongings.first.update_attributes(:attr => :val)
如何/为什么?!一旦关联已经存在,为什么不评估 'after_save' 回调中的条件?
【问题讨论】:
标签: ruby-on-rails ruby polymorphism associations sti