jared
2008-Apr-11 22:28 UTC
polymorphic associations wrong when used with inherited class?
Consider the following: class Event belongs_to :event_object, :polymorphic => true end class Asset < ActiveRecord::Base end class EventedAsset < Asset has_one :event, :as => :event_object end EventedAsset.find(:first).event generates the following SQL: SELECT * FROM `events` WHERE (events.event_object_id = 1 AND events.event_object_type = ''Asset'') LIMIT 1 shouldn''t this be: SELECT * FROM `events` WHERE (events.event_object_id = 1 AND events.event_object_type = ''EventedAsset'') LIMIT 1 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jared
2008-Apr-11 22:46 UTC
Re: polymorphic associations wrong when used with inherited class?
I guess i should RTFM. (here for anyone with the same problem: http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html) Still, IMO, you shouldn''t have to define a crazy method like: def attachable_type=(sType) super(sType.to_s.classify.constantize.base_class.to_s) end to make STI work with polymorphic associations, it should "just work," like everything else does in rails. On Apr 11, 3:28 pm, jared <jared.mo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Consider the following: > > class Event > belongs_to :event_object, :polymorphic => true > end > > class Asset < ActiveRecord::Base > end > > class EventedAsset < Asset > has_one :event, :as => :event_object > end > > EventedAsset.find(:first).event generates the following SQL: > > SELECT * FROM `events` WHERE (events.event_object_id = 1 AND > events.event_object_type = ''Asset'') LIMIT 1 > > shouldn''t this be: > > SELECT * FROM `events` WHERE (events.event_object_id = 1 AND > events.event_object_type = ''EventedAsset'') LIMIT 1--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---