我在模型中有两个作用域。两者都使用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/