mysql - 如何使用 ActiveRecord 进行两次连接并执行单个事件记录查询?

标签 mysql ruby-on-rails ruby activerecord

我正在使用 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/

相关文章:

mysql - 为什么 PHP artisan migrate 命令显示错误?

mysql - 将 SQL 子查询重写为 JOIN?

ruby-on-rails - Rails 找不到生成器类

ruby-on-rails - 如何在单个字段上应用多个正则表达式

ruby - 为什么 rvm install ruby​​ 不起作用?

mysql - 表MYSQL查询

mysql - 如何使用开源 kafka connect 从 Aurora 连接 MSK

ruby-on-rails - Rails 4 + Devise - 更新用户帐户时不允许的参数

ruby-on-rails - 是否可以在 before_enqueue 回调中访问作业的参数?

ruby-on-rails - 是否应该对 Rails Controller 进行验证测试?