Forum: Ruby on Rails Sending messages between users

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5cabbcab339feace80762194dc9302ed?d=identicon&s=25 Christoph Jasinski (Guest)
on 2009-02-20 02:36
(Received via mailing list)

I'm trying to create an web app where users can write messages to each
other. I use the restful_authentication plugin for the users. So they
an user_id. How ever the messages should have a:
- sender
- receiver
- content

As i saw in some screencast the association use the user_id to connect
to something. But in my model there would be 2 user_id column which
obviously doesn't work. So how do I do it?

My workaround was to create a receiver and sender model which contained
user_id and a message_id. Then the message contained a sender_id,
receiver_id and the content. However now I'm stuck somehow. *Maybe it's
because 2:30am here and I should be in bed*. I don't know. Is there an
easier workaround? Some tutorials, maybe?

How do I collect my mails? Have a controller named messages where I
this, but it doesn't work (I thing my association declaration are the
@inbox = current_user.message.find_by_receiver_id(:user)

*The Models:*

class *Message* < ActiveRecord::Base
  belongs_to :sender
  has_one :receiver

  validates_presence_of :sender
  validates_presence_of :receiver
  validates_length_of :content, :maximum => 4096

class *Receiver* < ActiveRecord::Base
  belongs_to :message

class *Sender* < ActiveRecord::Base
  has_many :message

class *User* < ActiveRecord::Base (restful_authentication plugin)
  # Virtual attribute for the unencrypted password
  attr_accessor :password
  attr_accessible :login, :email, :password, :password_confirmation

Greetings and thanks for any kind of help

Fac81a9d95c81d817a2debb6f717f3ac?d=identicon&s=25 Sazima (Guest)
on 2009-02-20 15:54
(Received via mailing list)

Why don't you use (or check how it's done) restful easy messages?
Alternatively, LOVD ( does it all.

Cheers, Sazima

On Feb 19, 10:35 pm, Christoph Jasinski
C4eb02ef0d5e1474bbfe5be14ed6adf8?d=identicon&s=25 Rakoth (Guest)
on 2009-02-20 16:20
(Received via mailing list)
Hello Chris, may I try to help you?
I thing you need something like this:

class *Message* < ActiveRecord::Base

  belongs_to :sender, :class_name => 'User', :foreign_key =>
  belongs_to :receiver, :class_name => 'User', :foreign_key =>


class *User* < ActiveRecord::Base (restful_authentication plugin)
  # Virtual attribute for the unencrypted password
  attr_accessor :password
  attr_accessible :login, :email, :password, :password_confirmation

  has_many :sent_messages, :class_name => 'Message' , :foreign_key =>
  has_many :received_messages, :class_name => 'Message' , :foreign_key
=> 'receiver_id'

and then you can do this:
@inbox = current_user.received_messages.find_by_receiver_id(:user)

I'm dont test this, but it shold work)
Sorry for my english.
This topic is locked and can not be replied to.