I have two classes Message and User. Message has sender_id and recipient_id both foreign keys for User. How to build relationship where I'll be able to get user for both sender and recipient, like @message.sender.name and @message.recipient.name
I tried to do it by this way:
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => 'User', :foreign_key => 'sender'
belongs_to :recipient, :class_name => 'User', :foreign_key => 'recipient'
end
class User < ActiveRecord::Base
has_many :recivied_messages, :class_name => 'Message', :foreign_key => 'recipient'
has_many :send_messages, :class_name => 'Message', :foreign_key => 'sender'
end
But it didn't help, when I'm trying to access to, for instance, @message.recipient.name it says that "undefined method `name'"