Problem with two foreign keys

so, I’m using following schema:

#Users#
id
nick

#Messages#
id
from_user_id
to_user_id
subject
message

#user.rb
has_many :messages

#message.rb
belongs_to :to_user, :class_name => “User”
belongs_to :from_user, :class_name => “User”

assigning the userids to a message is no problem, but is there a way to
collect all messages received by a user?
like “user.messages” receives all messages with to_user_id = user.id?

On Jul 7, 2:59 am, Skave R. [email protected] wrote:

like “user.messages” receives all messages with to_user_id = user.id?
Your has_many needs to use the :foreign_key option. I put some
examples at
http://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table

Fred

Frederick C. wrote:

On Jul 7, 2:59�am, Skave R. [email protected] wrote:

like “user.messages” receives all messages with to_user_id = user.id?
Your has_many needs to use the :foreign_key option. I put some
examples at
http://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table

Fred

thanks. I forgot to add the associations to user.rb - I just used
has_many :messages.

It works fine now

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