ActiveRecord、has_many :through 和多态关联 [英] ActiveRecord, has_many :through, and Polymorphic Associations

查看:21
本文介绍了ActiveRecord、has_many :through 和多态关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位,

想确保我理解正确.并且请忽略此处的继承情况(SentientBeing),而是尝试将重点放在 has_many 中的多态模型:通过关系.也就是说,请考虑以下...

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. 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

在一个完美的世界中,给定一个 Widget 和一个 Person,我想执行以下操作:

In a perfect world, I'd like to, given a Widget and a Person, do something like:

widget.people << my_person

但是,当我这样做时,我注意到 widget_groupings 中grouper"的type"始终为空.但是,如果我像下面这样:

However, when I do this, I've noticed the 'type' of the 'grouper' is always null in widget_groupings. 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.

感谢您的帮助!

推荐答案

有一个 Rails 3.1.1 的已知问题破坏了此功能.如果您遇到此问题,请先尝试升级,它已在 3.1.2 中修复

There is a known issue with Rails 3.1.1 that breaks this functionality. If you are having this problem first try upgrading, it's been fixed in 3.1.2

你离我很近.问题是您滥用了 :source 选项.:source 应该指向多态的belongs_to 关系.然后您需要做的就是为您尝试定义的关系指定 :source_type.

You're so close. The problem is you're misusing the :source option. :source should points to the polymorphic belongs_to relationship. Then all you need to do is specify :source_type for the relationship you're trying to define.

对 Widget 模型的这一修复应该可以让您完全按照自己的意愿行事.

This fix to the Widget model should allow you do exactly what you're looking for.

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

这篇关于ActiveRecord、has_many :through 和多态关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆