Rails add two scope with active record result

这一生的挚爱 提交于 2019-12-23 12:58:30

问题


I am using acts-as-taggable-on gem

i use single field to search by tag_name and user_name

User.rb

class User < ActiveRecord::Base

  acts_as_taggable
  attr_accessor: :user_name, :age, :country, tag_list
  scope :tagged_with, lambda { |tag|
    {
      :joins => "INNER JOIN taggings ON taggings.taggable_id = user.id\
               INNER JOIN tags ON tags.id = taggings.tag_id AND taggings.taggable_type = 'User'",
      :conditions => ["tags.name = ?", tag],
      :order => 'id ASC'
    }
  }
  def self.search(search)
    if search
      where('name LIKE ?', "%#{search}%") + tagged_with(search)
    else
      scoped
    end
  end
end

But i have pagination issue while getting this as Array and i used "will_paginate/Array" in config/initializer/will_paginate.rb it doesn't work.

User controller

class UserController < ActionController::Base
  def index
    @users = User.search(params[:search]).paginate(:per_page => per_page, :page => params[:page])
  end

Console.

User.search("best") => Should search by both tag_name and user_name and return ActiveRecord result.

i want to get the result of User.search("best") union with result of tag name User.tagged_with("best")

Can you help me to add this scopes as ActiveRecord relation to use pagination without issue.


回答1:


I think you just need to return a chainable scope (use . instead of +):

where('name LIKE ?', "%#{search}%").tagged_with(search)

It returns an ActiveRecord::Relation instead of an Array.

If you need to perform an UNION operation, I recommend you to follow this thread: ActiveRecord Query Union.

One possible approach is to extend ActiveRecord:

module ActiveRecord::UnionScope
  def self.included(base)
    base.send(:extend, ClassMethods)
  end

  module ClassMethods
    def union_scope(*scopes)
      id_column = "#{table_name}.id"
      sub_query = scopes.map { |s| s.select(id_column).to_sql }.join(" UNION ")
      where("#{id_column} IN (#{sub_query})")
    end
  end
end 

Usage (not tested):

class User < ActiveRecord::Base
  include ActiveRecord::UnionScope

  def self.search(search)
    if search
      union_scope(where('name LIKE ?', "%#{search}%"), tagged_with(search))
    else
      scoped
    end
  end
end


来源:https://stackoverflow.com/questions/33259409/rails-add-two-scope-with-active-record-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!