我希望找到一种基于has_many through关联子级模型的查询方式。
我有三个模型:
class Conversation < ActiveRecord::Base
has_many :conversations_participants
has_many :participants, through: :conversations_participants
end
class ConversationsParticipant < ActiveRecord::Base
belongs_to :conversation
belongs_to :participant, class_name: 'User'
end
class User < ActiveRecord::Base
has_many :conversations_participants
has_many :conversations, through: :conversations_participants
end
我需要找到参与者匹配一个ID数组的对话。
目前我的代码如下(无法工作):
Conversation.includes(:participants).where(participants: params[:participants])