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?

Your has_many needs to use the :foreign_key option. I put some examples at Creating multiple associations with the same table - Space Vatican

Fred

Frederick Cheung wrote: