简体   繁体   English

ActiveRecord,has_many:through和Polymorphic Associations

[英]ActiveRecord, has_many :through, and Polymorphic Associations

Folks, 伙计们,

Want to make sure I understand this correctly. 想确保我理解正确。 And please disregard the case for inheritance here (SentientBeing), trying to instead focus on polymorphic models in has_many :through relationships. 请忽略继承的情况(SentientBeing),尝试着重于has_many中的多态模型:通过关系。 That said, consider the following... 也就是说,考虑以下......

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

In a perfect world, I'd like to, given a Widget and a Person, do something like: 在一个完美的世界里,我想,给一个Widget和一个人,做一些像:

widget.people << my_person

However, when I do this, I've noticed the 'type' of the 'grouper' is always null in widget_groupings. 但是,当我这样做时,我注意到'grouper'的'type'在widget_groupings中始终为null。 However, if I to something like the following: 但是,如果我喜欢以下内容:

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. 我不认为我曾经见过这种非多态关联,只是想知道这是否是特定于这个用例的东西,或者我是否有可能盯着一个bug。

Thanks for any help! 谢谢你的帮助!

There is a known issue with Rails 3.1.1 that breaks this functionality. Rails 3.1.1存在一个已知问题 ,它破坏了这一功能。 If you are having this problem first try upgrading, it's been fixed in 3.1.2 如果您遇到此问题首先尝试升级,则已在3.1.2中修复

You're so close. 你真是太近了 The problem is you're misusing the :source option. 问题是你误用了:source选项。 :source should points to the polymorphic belongs_to relationship. :source应该指向多态的belongs_to关系。 Then all you need to do is specify :source_type for the relationship you're trying to define. 然后,您需要做的就是为您尝试定义的关系指定:source_type。

This fix to the Widget model should allow you do exactly what you're looking for. 对Widget模型的此修复应该允许您完成您正在寻找的内容。

class Widget < ActiveRecord::Base
  has_many :widget_groupings

  has_many :people, :through => :widget_groupings, :source => :grouper, :source_type => 'Person'
  has_many :aliens, :through => :widget_groupings, :source => :grouper, :source_type => 'Alien'
end

如上所述,这不适用于rails 3.1.1,因为上面有一个bug:source,但它已在Rails 3.1.2中修复

has many :through and polymorphic don't work together. 有很多:通过和多态不一起工作。 If you try to access them directly, it should throw an error. 如果您尝试直接访问它们,则应该抛出错误。 If i am not mistaken, you have to hand write widget.people and the push routine. 如果我没有弄错的话,你必须亲自编写widget.people和推送例程。

I don't think it is a bug, it is just something which hasn't been implemented yet. 我不认为这是一个错误,它只是尚未实现的东西。 I would imagine we see it in the feature, because everyone has a case in which they could use it. 我想我们会在功能中看到它,因为每个人都有一个可以使用它的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM