I want to model messages between users, here is the requirement:
User has received and sent messages, and should be retrieved by
user.received_messages
anduser.sent_messages
Message has sender and receiver, and should be retrieved by
message.sender
andmessage.receiver
.
I have created the User model as:
script/generate model User name:string
and Message model as:
script/generate model Message content:text sender_id:integer receiver_id:integer
I have come up with the Message like below, and it works as wish
class Message < ActiveRecord::Base
belongs_to :sender, :class_name=>'User', :foreign_key=>'sender_id'
belongs_to :receiver, :class_name=>'User', :foreign_key=>'receiver_id'
end
but I don't know how to model the User, any advise is appreciated.