ruby-on-rails - ArgumentError : Relation passed to #or must be structurally compatible.值不兼容:[:joins]

标签 ruby-on-rails ruby

我在模型中有两个作用域。两者都使用joins。看来joins与Rails 5或查询不兼容。

例子:

class Blog < ApplicationRecord

  has_many :comments

  scope :with_comment_likes, -> {joins(:comments).merge(Comment.some_scope_on_comment)}
  scope :some_other_comment_merge_scope, -> {joins(:comments).merge(Comment.other_scope)}

  scope :aggregate_or_scope, -> {with_comment_likes.or(some_other_comment_merge_scope)}
end

Blog.aggregate_or_scope

返回错误:
ArgumentError: Relation passed to #or must be structurally compatible. 
Incompatible values: [:joins]

关于如何解决这个问题的任何建议?我很困惑我确实看到了this question,但是在应用它时遇到了麻烦。

最佳答案

我遇到了同样的问题,发现的解决方案是:
def matching_one_or_two temp = Model.matching_one + Model.matching_two Model.where('id in (?)',temp.map(&:id)) end
当然,这并不是世界上最高的性能,但是它会导致一个ActiveRecord::Relation对象指向“OR”结果。

关于ruby-on-rails - ArgumentError : Relation passed to #or must be structurally compatible.值不兼容:[:joins],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46248509/

相关文章:

javascript - 在rails中使用远程以ajax渲染形式执行脚本

ruby-on-rails - 将大型数据集导入数据库

ruby 公案 202 : Why does the correct answer give a syntax error?

ruby - 奇怪的 Ruby 行为

ruby-on-rails - Ruby:简洁的内联赋值?

ruby-on-rails - Rails 6 中是否必须手动将 ApplicationHelper 包含在 ApplicationController 中?

ruby-on-rails - 下载使用 Paperclip 附加的图像

javascript - 是否有可以将 JavaScript 插入现有 PDF 文件的 Ruby gem?

Ruby 调试输出 - 轻松打开/关闭

java - Ruby(服务器)- Java(客户端)连接错误