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