A .send method for associated models?

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

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.

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.