So you practically reference the other class twice in both Models, but
with different assiciation names (:sender <-> :recipient and
:sentmessages <-> :rcvdmessages) and foreig keys ( sender_id <->
recipient_id).
That creates two different associations with the same Model ....
Someone please correct me if i'm wrong, i never tested it and only made
my own thoughts from the rails docs
As i want to use soemthing like this myself soon, and IF the above is
correct, i want to ask:
How is the best way to store the messages twice, so sender + recipient
can both keep a copy and delete it whenever they want?
Anyone got any idea?
if @message.save
flash[:notice] = "Message Sent"
redirect_to :action => "index"
else
flash[:notice] = "Message not Sent"
redirect_to :action => "index"
end
end
both the sender and the userid are set to the wrong values in the
database. The sender always has a value of 0 in the database. The
userid always has a value of 1.
Hang on. You should only have one column in your 'messages' table that
relates to this join, and that column should be 'user_id'. You
shouldn't have a column that called 'sender'.
As far as I can tell, that code should work fine, as long as
current_user contains a saved User object.
When you say "The sender always has a value of 0 in the database. The
userid always has a value of 1.", exactly which tables and columns are
you talking about?
i think you got something twisted up.
-> in the model, you say, the sender's foreign key is "user_id"
-> in the controller, you assign the recipient ( params[:message][:to])
to the column "user_id"
From what you described i guess you have a foreign_key "sender" in your
message table that shold hold the ID of the user who sent the message,
and the "user_id" filed should hold the name of the recipient of the
message
Your column naming could be more self-explanatory, e.g. use
"recipient_id" as i suggested in my above post
but you should give some more info, like your exact table layout etc
...
and what do the parameters hold? i guess params[:message][:to] hold the
recipients name. well you need that user's id, not his name.
But im just guessing here, not enough insight and info....