【发布时间】:2015-01-08 16:06:41
【问题描述】:
我需要创建一个包含来自下表的数据的单个查询:
*对话:将用户之间的消息分组的模型
class Conversation < ActiveRecord::Base
# Associations
has_many :messages, dependent: :destroy
has_many :conversation_participants
has_many :users, :through => :conversation_participants
## Attributes title, created_at, updated_at
end
* ConversationParticipant:跟踪对话用户的模型
class ConversationParticipant < ActiveRecord::Base
## Associations
belongs_to :conversation
belongs_to :user
## Attributes conversation_id, user_id, seen, created_at, updated_at
end
* 消息:跟踪内容和发件人的模型
class Message < ActiveRecord::Base
belongs_to :conversation
belongs_to :sender, :class_name => "User"
## Attributes sender_id, content, conversation_id, created_at, updated_at
end
*User:具有name属性的模型
如何在单个查询中获取以下内容?
- limit (5) 条来自 uniq Conversation Message 的近期消息
- 哪里
user_id= current_user.id 来自 ConversationParticipant- 订购
seen=false,然后来自 ConversationParticipant 的updated_at DESC- 包括 对话
- 包括(消息发件人)=> 用户
- 包括来自 ConversationParticipant => 用户的其他参与者
注意:includes 和 select 很重要,因为这个问题旨在减少查询次数.
【问题讨论】:
-
Message 有 ConversationParticipant 而不是 User 有意义吗?另外,我不清楚您在 1 号下的 uniq 对话消息是什么意思。您目前使用的是什么?查看您当前的查询可能会帮助我了解您的目标。此外,查看您期望返回的对象的外观也可能会有所帮助。此外,这可能是数据库视图的一个很好的候选者,具体取决于您想用这些信息做什么,并且可以大大简化查询......
标签: mysql ruby-on-rails postgresql activerecord ruby-on-rails-4