首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ActiveRecord、has_many :直通和多态关联

ActiveRecord、has_many :直通和多态关联
EN

Stack Overflow用户
提问于 2009-11-06 04:12:58
回答 2查看 21.4K关注 0票数 117

各位朋友:

我想确认一下我的理解是否正确。请忽略此处的继承情况(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。

谢谢你的帮助!

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1683265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档