Hi, I am having trouble using STI and polymorphism together. I tried "has_many :as" without success so I am trying to be more explicit. Here is what I have now class Visual < ActiveRecord::Base has_many :tags, :foreign_key=>''resource_id'', :conditions=>[''resource_type = ?'', self.class.to_s], :dependent=>:destroy end class Picture < Visual end ==== I see generated SQL like the following SELECT * FROM tags WHERE (tags.resource_id = 1 AND (resource_type = ''Class'')) And I want to see SELECT * FROM tags WHERE (tags.resource_id = 1 AND (resource_type ''Picture'')) How can I adjust the conditions line so that I get Picture not Class? Thanks, Peter --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Peter Michaux wrote:> Hi, > > I am having trouble using STI and polymorphism together. I tried > "has_many :as" without success so I am trying to be more explicit. > Here is what I have now > > class Visual < ActiveRecord::Base > has_many :tags, > :foreign_key=>''resource_id'', > :conditions=>[''resource_type = ?'', self.class.to_s], > :dependent=>:destroy > end > > class Picture < Visual > end > > ====> > I see generated SQL like the following > > SELECT * FROM tags WHERE (tags.resource_id = 1 AND (resource_type = > ''Class'')) > > And I want to see > > SELECT * FROM tags WHERE (tags.resource_id = 1 AND (resource_type > ''Picture'')) > > How can I adjust the conditions line so that I get Picture not Class?This seems to be right up there with "never get involved in a land war in Asia" as a classic blunder. When you are using STI, the type information is stored in the table of the STI model. You don''t use a belongs_to :x, :polymorphic => :true if the X is using STI. Both STI and :polymorphic => :true allow for polymorphism, but they use separate and incompatible mechanisms. Choose one or the other and you''ll be fine. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 8/31/06, Josh Susser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Peter Michaux wrote: > > Hi, > > > > I am having trouble using STI and polymorphism together. I tried > > "has_many :as" without success so I am trying to be more explicit. > > Here is what I have now > > > > class Visual < ActiveRecord::Base > > has_many :tags, > > :foreign_key=>''resource_id'', > > :conditions=>[''resource_type = ?'', self.class.to_s], > > :dependent=>:destroy > > end > > > > class Picture < Visual > > end > > > > ====> > > > I see generated SQL like the following > > > > SELECT * FROM tags WHERE (tags.resource_id = 1 AND (resource_type > > ''Class'')) > > > > And I want to see > > > > SELECT * FROM tags WHERE (tags.resource_id = 1 AND (resource_type > > ''Picture'')) > > > > How can I adjust the conditions line so that I get Picture not Class? > > This seems to be right up there with "never get involved in a land war > in Asia" as a classic blunder. When you are using STI, the type > information is stored in the table of the STI model. You don''t use a > belongs_to :x, :polymorphic => :true if the X is using STI. Both STI and > :polymorphic => :true allow for polymorphism, but they use separate and > incompatible mechanisms. Choose one or the other and you''ll be fine.Hi Josh, Thanks for the reply. The belongs_to side of things seems to work just fine unless i haven''t worked into if far enough yet. It is the has_many side that causes me the grief. I thought the following would work regardless of a STI or Polymophic existance. has_many :tags, :foreign_key=>''resource_id'', :conditions=>[''resource_type = ?'', self.class.to_s], :dependent=>:destroy I thought in the :conditions of the has_many there would be a way to use the attributes of the model that actually has_many. Is that possible? Other things like :conditions=>[''blahblah = ?'', self.id], :conditions=>[''random_attribute = ?'', self.foo], Thanks again, Peter --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, Can anyone confirm that what I thought would work below absoultely will not work? That is a has_many :conditions cannot depend on a particular instances attributes. Thanks, Peter On 8/31/06, Peter Michaux <petermichaux-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I thought the following would work regardless of a STI or Polymophic existance. > > has_many :tags, > :foreign_key=>''resource_id'', > :conditions=>[''resource_type = ?'', self.class.to_s], > :dependent=>:destroy > > I thought in the :conditions of the has_many there would be a way to > use the attributes of the model that actually has_many. Is that > possible? > > Other things like > > :conditions=>[''blahblah = ?'', self.id], > :conditions=>[''random_attribute = ?'', self.foo], > > Thanks again, > Peter >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---