class Message < ActiveRecord::Base belongs_to :user end
class MessageRecipient < ActiveRecord::Base belongs_to :message belongs_to :message_recipientable, polymorphic: true end
class User < ActiveRecord::Base has_many :message_recipients, as: :message_recipientable has_many :messages, through: :message_recipients, source: :message end
class Group < ActiveRecord::Base has_many :message_recipients, as: :message_recipientable has_many :messages, through: :message_recipients, source: :message end
我要做的是类似 QQ 的聊天应用,有群聊和单聊
我的现在遇到的问题是:在不写纯 SQL 的情况下,怎么一次操作拿到单聊的两个人的 messages
如拿 user1 和 user2 的 messages
user1.massages.where user_id: user2.id //user2 发给 user1 的 mesaage
user2.massages.where user_id: user1.id //user1 发给 user2 的 mesaage
怎么样整合这两句???