Rails添加两个具有活动记录结果的范围

时间:2015-10-21 12:31:39

标签: ruby-on-rails ruby ruby-on-rails-3 rails-activerecord acts-as-taggable-on

我正在使用act-as-taggable-on gem

我使用单个字段按tag_name和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

但是我有分页问题,​​而得到它作为数组,我使用&#34; will_paginate / Array&#34;在config / initializer / will_paginate.rb中,它不起作用。

用户控制器

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

控制台。

  

User.search(&#34; best&#34;)=&gt;应该同时使用tag_name和user_name进行搜索并返回ActiveRecord结果。

我希望将User.search(&#34; best&#34;)联合的结果与标签名称User.tagged_with(&#34; best&#34;)

的结果联系起来

您能否帮助我将此范围添加为ActiveRecord关系,以便毫无问题地使用分页。

1 个答案:

答案 0 :(得分:3)

我认为你只需要返回一个可链接的范围(使用.代替+):

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

它返回ActiveRecord::Relation而不是Array

如果您需要执行UNION操作,建议您按照以下主题进行操作:ActiveRecord Query Union

一种可能的方法是扩展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 

用法(未经测试):

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