[英]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
我正在应用程序中设置私人消息传递,并且想知道实现此目标的最佳方法是什么,我有一个包含2列的
message
模型,即to_id
和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 the user
model? 我将如何在
user
模型中建立关系? i want to have a :received messages
association and a :sent_messages
Association. 我想建立一个
:received messages
关联和一个:sent_messages
关联。
Thanks 谢谢
Even if this tutorial was made for Rails 2, the logic is still useful: 即使本教程是针对Rails 2编写的,该逻辑仍然有用:
http://www.novawave.net/public/rails_messaging_tutorial.html http://www.novawave.net/public/rails_messaging_tutorial.html
You can specify the :foreign_key
on both sides, like this: 您可以在两侧指定
:foreign_key
,如下所示:
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. 我知道Rails 3有一个
:inverse_of
选项,您也可以通过,但我对此没有任何经验。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.