我有三个AR模型
class MessageThread < ActiveRecord::Base
has_many :message_threads_users, dependent: :destroy
has_many :users, through: :message_threads_users
end
class User < ActiveRecord::Base
has_many :message_threads_users, dependent: :destroy
has_many :message_threads, through: :message_threads_users
end
class MessageThreadsUser < ActiveRecord::Base
belongs_to :message_thread
belongs_to :user
end
给定一个user_id数组(即[5, 6, 7]
)我想找到或创建一个message_thread对象。我似乎无法在ActiveRecord Query API中找到一种好方法。有办法吗?
提前谢谢!
答案 0 :(得分:0)
我相信您必须与MessageThread
加入User
然后才有一个where子句。之后,如果您愿意,可以使用find
来获得更多特异性。
也许是这样的:
message_thread = MessageThread.joins(:users).where(users: { id: [5, 6, 7] })