FuelXC
2011-Apr-05 09:54 UTC
[RAILS] Why does AR use base_class for polymorphic relationship "type"
I have a couple models that inherit from a base class that then inherits from AR. When they are used in a polymorphic relationship the "type" is always the base class. Just curious if anyone has a reason why it doesn''t use the *actual* class rather then the base_class. I have patched AR to use the actual class if !base_class.table_exists? but it adds another query each time (which in turn fails some of the rails tests). My only guess was for use with STI, but even then I am not sure if there is a better solution then using the base_class -Eric -- 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.
Matt Jones
2011-Apr-06 14:30 UTC
Re: Why does AR use base_class for polymorphic relationship "type"
On Apr 5, 5:54 am, FuelXC <e...-BSXVcfXPjW3QT0dZR+AlfA@public.gmane.org> wrote:> I have a couple models that inherit from a base class that then > inherits from AR. > > When they are used in a polymorphic relationship the "type" is always > the base class. > > Just curious if anyone has a reason why it doesn''t use the *actual* > class rather then the base_class. I have patched AR to use the actual > class if !base_class.table_exists? but it adds another query each time > (which in turn fails some of the rails tests). > > My only guess was for use with STI, but even then I am not sure if > there is a better solution then using the base_classBy default, Rails stores the class name that corresponds to the *table* name that the record is from (to avoid storing duplicate data for STI subclasses). If you''re actually storing the subclasses in different tables, you''ll probably want to do ''self.abstract_class true'' in the base class to make this clearer. --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.