Multiple relations between two models

Well i thought about this issue too sometime ... and though i'm
perfectly sure, this could work:

class Message < ActiveRecord::Base
    belongs_to :sender, :foreign_key => "sender_id", :class => "User"
    belongs_to :recipient, :foreign_key => "recipient_id", :class =>
"User"
end

class User < ActiveRecord::Base
   has_many :sentmessages, :foreign_key => "sender_id", :class =>
"Message"
   has_many :rcvdmessages, :foreign_key => "recipient_id", :class =>
"Message"
end

Then you *should* be able to do:

somevariable = message.sender
somevariable = message.recipient

sentmsgs = user.sentmessages.find :all
rcvdmsgs = user.rcvdmessages.find :all

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?

Stewart wrote:

It's possible I am not as smart as they think I am.

class Message < ActiveRecord::Base
  belongs_to :sender, :class_name => "User", :foreign_key => "user_id"
end

the above line of code works fine for me when I do something like
this...

<%= message.sender.full_alpha_name %>

however, when I am trying to create a new message with the following
code...

  def sendmessage
    @message = Message.new()

    @message.body = params[:message][:body]
    @message.title = params[:message][:title]
    @message.user_id = params[:message][:to]
    @message.sender = current_user

    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?

Chris

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

i think, your :belongs_to sould look like this:

belongs_to :sender, :class_name "User", :foreign_key => "sender"
belongs_to :recipient, :class_name "User", :foreign_key => "user_id"

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....