各位,
我想确定我没理解错。请忽略这里继承的情况(SentientBeing) ,试图将重点放在 has _ many: through 关系中的多态模型上。也就是说,考虑以下..。
class Widget < ActiveRecord::Base
has_many :widget_groupings
has_many :people, :through => :widget_groupings, :source => :person, :conditions => "widget_groupings.grouper_type = 'Person'"
has_many :aliens, :through => :widget_groupings, :source => :alien, :conditions => "video_groupings.grouper_type = 'Alien'"
end
class Person < ActiveRecord::Base
has_many :widget_groupings, :as => grouper
has_many :widgets, :through => :widget_groupings
end
class Alien < ActiveRecord::Base
has_many :widget_groupings, :as => grouper
has_many :widgets, :through => :widget_groupings
end
class WidgetGrouping < ActiveRecord::Base
belongs_to :widget
belongs_to :grouper, :polymorphic => true
end
在一个完美的世界中,我希望在给定一个 Widget 和一个 Person 的情况下,执行以下操作:
widget.people << my_person
但是,当我这样做的时候,我注意到在 widget _ groupings 中,‘ grouper’的‘ type’总是 null。但是,如果我这样做:
widget.widget_groupings << WidgetGrouping.new({:widget => self, :person => my_person})
Then all works as I would have normally expected. I don't think I've ever seen this occur with non polymorphic associations and just wanted to know if this was something specific to this use case or if I'm potentially staring at a bug.
谢谢你的帮助!