Modeling messages between users
I want to simulate messages between users, this is what:
-
User received and sent messages and must be found
user.received_messages
anduser.sent_messages
-
The message has a sender and a recipient and must be received
message.sender
andmessage.receiver
.
I created a User model as:
script/generate model User name:string
and the message model as:
script/generate model Message content:text sender_id:integer receiver_id:integer
I came up with a Post as below and it works like a 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 dont know how to model a user, any advice is appreciated.
source to share
@ eric2323223
If you just copied / pasted the code from Milan, then I suspect that the error you are seeing is that there is no half-line before "foreign_key".
The lines should read:
has_many :sent_messages, :class_name => "Message", :foreign_key => "sender_id"
has_many :received_messages, :class_name => "Message", :foreign_key => "receiver_id"
Kenny
source to share