各位朋友:
我想确认一下我的理解是否正确。请忽略此处的继承情况(SentientBeing),转而关注has_many中的多态模型:通过关系。也就是说,请考虑以下几点:
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’总是空的。但是,如果我想要类似于以下内容:
widget.widget_groupings << WidgetGrouping.new({:widget => self, :person => my_person})
然后,所有的工作就像我通常预期的那样。我认为我从来没有在非多态关联中看到过这种情况,我只是想知道这是不是这个用例特有的东西,或者我可能是在盯着一个bug。
谢谢你的帮助!
发布于 2011-12-04 21:02:06
如上所述,由于:source上的错误,这在rails 3.1.1中不起作用,但在Rails 3.1.2中已修复
发布于 2009-11-06 07:21:51
有很多:透传和多态不能一起工作。如果您尝试直接访问它们,应该会抛出一个错误。如果我没记错的话,您必须手写widget.people和push例程。
我不认为这是一个bug,它只是还没有实现的东西。我想我们会在功能中看到它,因为每个人都有自己可以使用它的情况。
https://stackoverflow.com/questions/1683265
复制相似问题