Model associations

Hello everyone,

I have a Model and table called ‘comments.’ This table has columns
‘id’, ‘user_id’, ‘commenter_id’, and ‘message.’ I can access the user
of the comment, like this “@commenter.user”, but cannot access the
commenter (the one who made the comment).

The commenter should is the same type object as user, so polymorphism
isn’t the solution. I thought that a foreign key would do the trick,
but I either set up the key incorrectly, or using a foreign isn’t the
solution.

Is there a way to access the commenter as a User object, like
@comment.commenter, without having to create a table/model called
Commenter?

Thanks for your time,
Wild Al

In comment.rb

belongs_to :commenter, :foreign_key => ‘commenter_id’, :class_name =>
‘User’

That should do it.

That did it, thanks a lot!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs