我需要一些帮助来查询这里(Rails 5.1)。我有一个名为Verification
( belongs_to :verifiable, polymorphic: true
) 的多态关联对象。其他对象,如Comment
,可以有verifications
( has_many :verifications, as: :verifiable
),最后一个(按 排序id
)被认为是当前的。Verification
有一个枚举属性status
。我需要检索具有特定user_id
(当前用户)或当前(即最后一个)验证对象且状态正确(例如,approved
)的评论。
所以,我正在尝试:
self.where(user_id: user.id).or(self.joins(:verifications).limit(1).where(verifications: { status: 'approved' }))
但是 Rails 给了我以下错误:
Relation passed to #or must be structurally compatible. Incompatible values: [:joins, :references, :limit]
我该怎么办?