ruby-on-rails - Ruby on Rails - 结合 #or 和 #joins
问题描述
我正在开发一个 RoR 应用程序,我遇到了以下问题:我有一个模型A
,该模型belongs_to
最多有另一个模型的两个单独实例B
,我想定义一个范围,该范围返回A
满足某些条件的所有实例B
使用范围的任一实例B
。伪代码:
class A < ApplicationRecord
belongs_to :first, class_name: "B", dependent: :destroy, optional: true
belongs_to :second, class_name: "B", dependent: :destroy, optional: true
# How to properly do this?
scope :A_scope, lambda { left_outer_joins(:first).merge(B.B_scope).or(left_outer_joins(:second).merge(B.B_scope)) }
end
在哪里:
class B < Application Record
scope :B_scope, lambda { .. }
end
我尝试了几种变体,但我一直遇到它的问题:运行但不返回我想要的,或者它给出ArgumentError
:
ArgumentError (Relation passed to #or must be structurally compatible. Incompatible values: [:left_outer_joins])
解决方案
推荐阅读
- angular - 在渲染模板之前等待两个 observables(包括失败的一个)
- laravel - laravel addSelectRaw() - 如何在 addSelect() 中绑定变量?
- java - Spring Security 在 REST 控制器上进行单元测试时导致问题
- python - TensorFlow Keras Softmax 层输出相对于输入多维
- c++ - 多次清理 OpenSSL 是否安全?
- python-3.x - 使用 selenium 运行自动喜欢帖子的机器人
- android - 如何使用另一个表从 firebase 表中获取数据
- vue.js - 模块存储重置为状态
- colors - 在 VS Code 自己的颜色主题中使用用户定义的颜色
- node.js - 启动新离子项目 Json 时出错