I have done a little polymorphic associations stuff and have refreshed my memory on it again. What it seems like is that if I have a particular record and I want to make it easy for many different parent records to associate with it using has_one or has_many, that is fine. If I want a parent record to have multiple kinds of children through one association, I don''t see how to do that (except maybe with STI but then they all have to share the same fields in one record). I figure there must be some way to do this with a kind of intermediate join or something, but I haven''t seen any examples that I can recall .. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Kendall Gifford
2011-Jul-21 00:10 UTC
Re: polymorphic assoc seems only best for multiple parents ?
On Wednesday, July 20, 2011 7:04:09 AM UTC-6, Jedrin wrote:> > > I have done a little polymorphic associations stuff and have > refreshed my memory on it again. > What it seems like is that if I have a particular record and I want to > make it easy for many different parent records to associate with it > using has_one or has_many, that is fine. > > If I want a parent record to have multiple kinds of children through > one association, I don''t see how to do that (except maybe with STI but > then they all have to share the same fields in one record). I figure > there must be some way to do this with a kind of intermediate join or > something, but I haven''t seen any examples that I can recall ..If I understand what you''re saying then no, it isn''t even possible using a join table. This is the quick example I tested with: # The following demonstrates that this DOES NOT work... class Region < ActiveRecord::Base # a.k.a the "parent" that wants to "own" several # different kinds of children through one relation (shapes) has_many :regions_shapes has_many :shapes, :through => :regions_shapes end class RegionsShape < ActiveRecord::Base # polymorphic join model belongs_to :region belongs_to :shape, :polymorphic => true end class Ellipse < ActiveRecord::Base # one of two types of "children" that can belong to a # Region (parent) through its "shapes" relation has_one :regions_shape, :as => :shape has_one :region, :through => :regions_shape end class Polygon < ActiveRecord::Base # the other of the two types of "children" has_one :regions_shape, :as => :shape has_one :region, :through => :regions_shape end All of the relationships work fine EXCEPT for the one you wanted, in this example Region#shapes. Trying to use it will raise: ActiveRecord::HasManyThroughAssociationPolymorphicError: Cannot have a has_many :through association ''Region#shapes'' on the polymorphic object ''Shape#shape'' I think you''d have to simply have several explicit relationships for each type that the parent can "have": class Region < ActiveRecord::Base has_many :ellipses has_many :polygons end class Polygon < ActiveRecord::Base belongs_to :region end class Ellipse < ActiveRecord::Base belongs_to :region end Then just create a "combining" method for each relation-like action that the various relation methods support. Since you aren''t working with actual relation objects, you can''t chain or further filter on the results though so.... def shapes polygons + ellipses # or something like that for an Array (not a relation) end I''m assuming this is because if #shapes *could* be/return a relation then said relation would really have to have the ability to map to _n_ seperate SQL select statements (one for each table that can possibly be a "child"). As such, further filtering/sorting/offset/limit operations on such a relation would be hard, not make sense, or be impossible. Anyone more knowledgeable know if I''m wrong here? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/JEYaqOQGEvYJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Matt Jones
2011-Jul-21 13:53 UTC
Re: polymorphic assoc seems only best for multiple parents ?
On Jul 20, 8:10 pm, Kendall Gifford <zettab...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''m assuming this is because if #shapes *could* be/return a relation then > said relation would really have to have the ability to map to _n_ seperate > SQL select statements (one for each table that can possibly be a "child"). > As such, further filtering/sorting/offset/limit operations on such a > relation would be hard, not make sense, or be impossible. > > Anyone more knowledgeable know if I''m wrong here?You''ve pretty much hit the nail on the head - this issue is exactly what the has_many_polymorphs plugin was created to handle. I can''t find a definitive repo for a version that works on Rails 3, though. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.