我有一个模型
class PatientProfile < ActiveRecord::Base
has_many :friendships
with_options through: :friendships, source: :friend do |model|
model.has_many :friends, -> { where(friendships: { status: 'accepted'}) }
model.has_many :friend_requests, -> { where(friendships: { status: 'requested'}) }
model.has_many :requested_friendships, -> { where(friendships: { status: 'requestor'}) }
enduids = ["123", "456"]
我想查询PatientProfile以查找uid在uids数组中的所有记录,并且这两个用户之间没有友谊的记录。
这应该可以通过join来实现,但是我在documentation中遇到了问题
编辑:
uid在PatientProfile上
在Friendships表上有两列:friend_id和patient_profile_id。我只想要PatientProfile,friend不在uids列表中,patient_profile_id也不是,比如说,1
发布于 2016-02-02 22:53:00
我们遗漏了一些上下文,关于你所说的没有友情记录的意思,但如果你正在寻找记录,没有与uids的友情,它将是:
PatientProfile.includes(:friendships).where(uid: [123, 456], friendships: {id: nil})更新
或者,如果您更喜欢作用域版本:
scope :no_friendships, -> { includes(:friendships).where(friendships: {id: nil}) }所以现在你可以这样做:
PatientProfile.no_friendships.where(uid: [123, 456])https://stackoverflow.com/questions/35156244
复制相似问题