Hi all,
I try to implement an internal mail system to my rails app. I found this
message [1] on another mailing list, but i need it to be a little
different.
So here’s the case : i got a user table, a message table, and a
communication table. This last one contains the message_id, the
sender_id and the receiver_id. I need it for some future improvment.
user.rb
has_many :communications, :dependent => :destroy
has_many :sent_messages, :class_name => “Message”, :through =>
:communications, :source => :sender, :order => ‘created_at DESC’
has_many :received_messages, :class_name => “Message”, :foreign_key =>
“receiver_id”, :order => ‘created_at DESC’
has_many :unread_messages, :class_name => “Message”, :foreign_key =>
“receiver_id”, :conditions => [‘read_at IS NULL’], :order => ‘created_at
DESC’
communication.rb
belongs_to :sender, :class_name => “User”, :foreign_key => “sender_id”
belongs_to :receiver, :class_name => “User”, :foreign_key
=>“receiver_id”
has_one :message
message.rb
belongs_to :communication
But when i try to get some_user.sent_message, it returns me this error :
ActiveRecord::StatementInvalid: PGError: ERROR: column
communications.user_id does not exist
LINE 1: …messages.id = communications.sender_id WHERE
(("communica…
^
: SELECT “messages”.* FROM “messages” INNER JOIN communications ON
messages.id = communications.sender_id WHERE
((“communications”.user_id = 1)) ORDER BY created_at DESC
Obviously, something went wrong and Rails try to get the
communications.user_id id place of communications.sender_id.
Any help would be appreciate. Thanks