Rails 3: Setting up private messaging in my rails app?
I am setting up private messaging in my application and was wondering what the best way to do this would be, I have a message
model that has 2 columns, a to_id
and from_id
I have the association set in the message model like so:
class Message < ActiveRecord::Base
belongs_to :recipient, :class_name => 'User', :foreign_key => "to_id"
belongs_to :sender, :class_name => 'User', :foreign_key => "from_id"
end
How would i set up the relationship in th开发者_开发问答e user
model? i want to have a :received messages
association and a :sent_messages
Association.
Thanks
Even if this tutorial was made for Rails 2, the logic is still useful:
http://www.novawave.net/public/rails_messaging_tutorial.html
You can specify the :foreign_key
on both sides, like this:
class User < ActiveRecord::Base
has_many :sent_messages, :class_name => 'Message', :foreign_key => 'from_id'
has_many :received_messages, :class_name => 'Message', :foreign_key => 'to_id'
end
I know Rails 3 has an :inverse_of
option you can pass as well, but I don't have any experience with that.
精彩评论