Forum: Ruby on Rails Sending messages between users

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Christoph J. (Guest)
on 2009-02-20 03:36
(Received via mailing list)
Hi,

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
have
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
users
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
a
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
tried
this, but it doesn't work (I thing my association declaration are the
cause):
@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
end

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

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

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


Greetings and thanks for any kind of help

Chris
Sazima (Guest)
on 2009-02-20 16:54
(Received via mailing list)
Chris,

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

Cheers, Sazima

On Feb 19, 10:35 pm, Christoph J.
Rakoth (Guest)
on 2009-02-20 17: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 =>
'sender_id'
  belongs_to :receiver, :class_name => 'User', :foreign_key =>
'receiver_id'

end

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 =>
'sender_id'
  has_many :received_messages, :class_name => 'Message' , :foreign_key
=> 'receiver_id'
  ...
end

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.