我正在使用 MySQL,我正在尝试创建几个范围以根据一组对话查找用户,用户和对话具有允许用户创作和接收对话的多态关联。
“with_conversations”范围正在做我想做的事,但是,它不是很有效,我无法让它执行单个查询而不是添加 2 个数组,这会触发 2 个单独的查询。
class User
scope :with_received_conversations, -> (conversations) { joins(:received_conversations).where(conversations: { id: conversations, receiver_type: "User" }) }
scope :with_authored_conversations, -> (conversations) { joins(:authored_conversations).where(conversations: { id: conversations, author_type: "User" }) }
scope :with_conversations, -> (conversations) { with_authored_conversations(conversations) | with_received_conversations(conversations) }
end
我尝试使用“or()”事件记录函数,但它返回以下错误:
*** ArgumentError Exception: Relation passed to #or must be structurally compatible. Incompatible values: [:joins]
nil
最佳答案
我不允许发表评论,所以我很抱歉创建一个答案。我认为这篇文章很有帮助。
Relation passed to #or must be structurally compatible. Incompatible values: [:references]
关于mysql - 如何使用 ActiveRecord 进行两次连接并执行单个事件记录查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58084363/