How do I combine results from two queries on the same model?

前端 未结 4 1978
南方客
南方客 2021-02-15 13:56

I need to return exactly ten records for use in a view. I have a highly restrictive query I\'d like to use, but I want a less restrictive query in place to fill in the results i

4条回答
  •  广开言路
    2021-02-15 14:48

    If you want an ActiveRecord::Relation object instead of an Array, you can use:

    • ActiveRecordUnion gem.

      Install gem: gem install active_record_union and use:

      def self.current
        Article.rescue_articles.union(Article.listed_articles).limit(10)
      end
      
    • UnionScope module.

      Create module UnionScope (lib/active_record/union_scope.rb).

      module ActiveRecord::UnionScope
        def self.included(base)
          base.send :extend, ClassMethods
        end
      
        module ClassMethods
          def union_scope(*scopes)
            id_column = "#{table_name}.id"
             if (sub_query = scopes.reject { |sc| sc.count == 0 }.map { |s|   "(#{s.select(id_column).to_sql})" }.join(" UNION ")).present?
              where "#{id_column} IN (#{sub_query})"
            else
              none
            end
          end
        end
      end
      

      Then call it in your Article model.

      class Article < ActiveRecord::Base
        include ActiveRecord::UnionScope
        ...
        def self.current
          union_scope(Article.rescue_articles, Article.listed_articles).limit(10)
        end
        ...
      end
      

提交回复
热议问题