has_many :through

I think this is a strong case for habtm, rather than has_many :through. I'm normally a big fan of :through and use it often, but I've come to realize that sometimes, habtm really is much more appropriate.

First, I'd rename your message_users table to messages_recipients (note: both message and recipient are plural), and change the user_id column in that table to recipient_id. Then.... (disclaimer: this code is untested, so there may be some typos and other goofs)

class Message < ActiveRecord::Base   belongs_to :sender, :class_name => 'User'   has_and_belongs_to_many :recipients, :class_name => 'User' end

class User < ActiveRecord::Base   has_many :sent_messages, :class_name => 'Message'   has_and_belongs_to_many :received_messages, :class_name => 'Message' end

now... @user.sent_messages @user.received_messages @message.sender @message.recipients


get rid of your MessageRecipient class. You don't need it.

Star Burger wrote: