Nei*_*eil 8 ruby ruby-on-rails
我在模型中有两个范围.都利用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
Run Code Online (Sandbox Code Playgroud)
返回错误:
ArgumentError: Relation passed to #or must be structurally compatible.
Incompatible values: [:joins]
Run Code Online (Sandbox Code Playgroud)
有关如何解决此问题的任何建议?我很难过.我确实看到了这个问题,但我在应用它时遇到了麻烦.
小智 1
我遇到了同样的问题,我发现的解决方案是:
def matching_one_or_two
temp = Model.matching_one + Model.matching_two
Model.where('id in (?)',temp.map(&:id))
end
当然不是世界上最好的性能,但它会导致 ActiveRecord::Relation 对象指向“OR”结果。