I have a comments table that I use to allow comments on various models in my app. Two fields in the comments table are parent_id and parent_type. I then have belongs_to statements with conditions pointing to the parent_type to give me various associated models. For example: belongs_to :event, :foreign_key => "parent_id", :conditions => ["parent_type = ?", "Event"] This allows me to do comment.event to get the parent event. I can also then do comment.event.user to get the user who created the event. In some cases where I am DRYing things up, I want to dynamically call the associated model''s user based on the parent_type field. Basically what I''m looking for is the syntatical equivalent of something like this: comment.send(comment.parent_type).user.first_name Is there such a thing? Is there a part of ActiveRecord I should be studying that would explain this? Any help would be greatly appreciated. --Dylan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
This is what I have implemented in my Comment model in the meantime. If there''s a nicer way to do it, I''m very interested. def parent Object.const_get(self.parent_type).find(self.parent_id) end Now I can access, for instance, comment.parent.user.first_name if I need to. But like I said, if there''s another way to do it (through Ruby itself), I''d love to know. --~--~---------~--~----~------------~-------~--~----~ 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 Dylan, If i understand what you are trying to do, i belive there is an easiest way to do it. Ive done like a comment engine for several models so the user can comment everything on the app, but i used polymorphic associations. Here is more info about it: http://wiki.rubyonrails.com/rails/pages/ HowToUsePolymorphicAssociations D. On Oct 11, 2006, at 5:41 PM, Dylan Bennett wrote:> > I have a comments table that I use to allow comments on various models > in my app. Two fields in the comments table are parent_id and > parent_type. I then have belongs_to statements with conditions > pointing > to the parent_type to give me various associated models. For example: > > belongs_to :event, :foreign_key => "parent_id", :conditions => > ["parent_type = ?", "Event"] > > This allows me to do comment.event to get the parent event. I can also > then do comment.event.user to get the user who created the event. > > In some cases where I am DRYing things up, I want to dynamically call > the associated model''s user based on the parent_type field. Basically > what I''m looking for is the syntatical equivalent of something like > this: > > comment.send(comment.parent_type).user.first_name > > Is there such a thing? Is there a part of ActiveRecord I should be > studying that would explain this? Any help would be greatly > appreciated. > > --Dylan > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---