我正在构建一个小型应用程序,用户可以在其中发送和接收消息。每条消息都有一个sender_id
和一个recipient_id
(都包含用户ID)
# Relevant lines of User model
has_many :sent_messages, :class_name => 'Message', :foreign_key => 'sender_id'
has_many :received_messages, :class_name => 'Message', :foreign_key => 'recipient_id'
我可以在我的用户模型中做什么来允许我调用@user.all_messages
,并返回该用户发送和接收的消息?
(我使用的是 Rails 3.2)