Forum: Ruby on Rails Internal mail system : model definition issue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Céd B. (Guest)
on 2009-03-31 13:09
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 ;-)


---
[1]
http://groups.google.com/group/rubyonrails-talk/br...
This topic is locked and can not be replied to.