Ruby on Rails:下拉搜索 - 类型为boolean的无效输入语法:“%%”

时间:2012-10-01 14:02:58

标签: ruby-on-rails ruby postgresql syntax

我正在尝试构建一个搜索功能,用户可以搜索不同的字段,例如。客户端,行业,技术使用下拉菜单。搜索功能一直有效,直到我决定允许用户一次搜索多种技术。

这是我的模特:

class Project < ActiveRecord::Base
  attr_accessible :tech2, :tech3, :tech4, :tech5, :edited_first_name, :edited_last_name, :first_name, :last_name, :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech



def self.search(search_client, search_industry, search_role, search_tech, search_tech2, search_tech3, search_tech4, search_tech5 , search_business_div, search_project_owner,  search_status, search_start_date_dd, search_start_date_A, search_start_date_B,  search_keywords) 
  return scoped unless search_client.present? || search_industry.present? || search_role.present? || search_tech.present? || search_tech2.present? || search_tech3.present? || search_tech4.present? || search_tech5.present? || search_business_div.present? || search_project_owner.present? || search_status.present? ||  search_keywords.present?


where(['client LIKE ? AND industry LIKE ? AND role LIKE ? AND (tech LIKE ? OR ? OR ? OR ? OR ? ) AND business_div LIKE ? AND project_owner LIKE ? AND status LIKE ? AND keywords LIKE ?', 
      "%#{search_client}%", "%#{search_industry}%" , "%#{search_role}%" , "%#{search_tech}%"  , "%#{search_tech2}%" , "%#{search_tech3}%"  , "%#{search_tech4}%" , "%#{search_tech5}%" ,"%#{search_business_div}%" , 
      "%#{search_project_owner}%"  , "%#{search_status}%", 
       "%#{search_keywords}%"
    ])



end


def self.paginated_for_index(projects_per_page, current_page)
    paginate(:per_page => projects_per_page, :page => current_page)
  end

end

这是我在项目控制器中的搜索操作:

def search


@search = params[:client], params[:industry], params[:role], params[:tech], params[:tech2],params[:tech3],params[:tech4],params[:tech5], params[:business_div], params[:project_owner], params[:status], params[:keywords]

@project_search = Project.search(*@search).order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page)

@search_performed = !@search.reject! { |c| c.blank? }.empty? 

   @project = Project.new(params[:project])


respond_to do |format|
      format.html # search.html.erb
      format.json { render :json => @project }
    end

end

尝试搜索技术时出现此错误:

ActiveRecord::StatementInvalid in Projects#search

    PG::Error: ERROR:  invalid input syntax for type boolean: "%%"
LINE 1: ...ND role LIKE '%%' AND (tech LIKE '%Telephony%' OR '%%' OR '%...
                                                             ^
: SELECT COUNT(*) FROM "projects"  WHERE (client LIKE '%%' AND industry LIKE '%%' AND role LIKE '%%' AND (tech LIKE '%Telephony%' OR '%%' OR '%%' OR '%%' OR '%%' ) AND business_div LIKE '%%' AND project_owner LIKE '%%' AND status LIKE '%%' AND keywords LIKE '%%')

Extracted source (around line #169):

166: 
167: <% if @project_search.total_entries > 0 %>
168: <% if @search_performed %>
169: 
170: <style>
171: CSS3 Code
172: 

我认为这与其他技术下拉菜单返回null有关。希望有人可以看到问题。提前谢谢。

更新

def self.like(text); "%#{text}%"; end

  def self.search(search_client, search_industry, search_role, search_tech, search_tech2, search_tech3, search_tech4, search_tech5, search_business_div, search_project_owner,  search_status, search_start_date_dd, search_start_date_A, search_start_date_B,  search_keywords)
    # start with a scoped query, to apply more scopes on it afterwards
    _projects = Project.scoped 
    # then, for each of the parameters, apply the scope only if present
    if search_client.present?
      _projects = _projects.where ['client LIKE ?', like(search_client)] 
    end
    if search_industry.present?
      _projects = _projects.where ['industry LIKE ?', like(search_industry)]
    end
    if search_role.present?
      _projects = _projects.where ['role LIKE ?', like(search_role)]
    end

    # here lies the problem. you search in the 'tech' column, but you seem to want to search in many columns...
    search_techs = [search_tech, search_tech2, 
          search_tech3, search_tech4, 
          search_tech5].select(&:present?).compact
    if search_techs.present?
      sql = (['tech LIKE ?'] * search_techs.size).join(" OR ")
      args = search_techs.collect {|t| like(t)}
      _projects = _projects.where [sql, *args]
    end
    if search_business_div.present?
      _projects = _projects.where ['business_div LIKE ?', like(search_business_div)]
    end
    if search_project_owner.present?
      _projects = _projects.where ['project_owner LIKE ?', like(search_project_owner)]
    end

     if search_status.present?
      _projects = _projects.where ['status LIKE ?', like(search_status)]
    end



todays_date = DateTime.now.to_date

if !search_start_date_A.blank? or !search_start_date_B.blank?
    search_start_date_A = Date.parse(search_start_date_A).strftime("%Y-%m-%d")
    search_start_date_B = Date.parse(search_start_date_B).strftime("%Y-%m-%d")
    todays_date = nil
    search_start_date_dd = nil

    end

if search_start_date_dd.blank?
    todays_date = nil
end

        if search_start_date_dd.blank?

      _projects = _projects.where [' DATE(start_date) BETWEEN ? AND ?', search_start_date_A, search_start_date_B]
    end


        if search_start_date_A.blank? or search_start_date_B.blank?
      _projects = _projects.where ['DATE(start_date) BETWEEN ? AND ?', search_start_date_dd, todays_date]
    end




    if search_keywords.present?
      _projects = _projects.where ['keywords LIKE ?', like(search_keywords)]
    end
    # now you have applied only the present scopes. return the result, and watch 
    # the query as it executes.
    _projects
  end

2 个答案:

答案 0 :(得分:2)

根据sql语法,查询无效。

tech LIKE '%1%' OR '%2%' OR '%3%'

请改为尝试:

tech LIKE '%1%' OR tech LIKE '%2%' OR tech LIKE '%3%'

答案 1 :(得分:1)

您的搜索方法可以写得更好,它可以帮助您找到问题(主要取决于无用的LIKE。

但是你正在使数据库不是他的工作。如此多的LIKE是任何RDBMS的墓碑。使用像solr这样的全文搜索工具,API更容易,而且效果会更好!

这里有一个几乎等效的search方法,只有在需要时才会添加每个LIKE。

class Project < ActiveRecord::Base

  def self.like(text); "%#{text}%"; end

  def self.search(search_client, search_industry, search_role, 
                  search_tech, search_tech2, search_tech3, search_tech4, 
                  search_tech5, search_business_div, search_project_owner,
                  search_status, search_start_date_dd, search_start_date_A, 
                  search_start_date_B,  search_keywords)
    # start with a scoped query, to apply more scopes on it afterwards
    _projects = Project.scoped 
    # then, for each of the parameters, apply the scope only if present
    if search_client.present?
      _projects = _projects.where ['client LIKE ?', like(search_client)] 
    end
    if search_industry.present?
      _projects = _projects.where ['industry LIKE ?', like(search_industry)]
    end
    if search_role.present?
      _projects = _projects.where ['role LIKE ?', like(search_role)]
    end
    if search_industry.present?
      _projects = _projects.where ['industry LIKE ?', like(search_industry)]
    end
    # here lies the problem. you search in the 'tech' column, but you seem to want to search in many columns...
    search_techs = [search_tech, search_tech2, 
          search_tech3, search_tech4, 
          search_tech5].select(&:present?).compact
    if search_techs.present?
      sql = (['tech LIKE ?'] * search_techs.size).join(" OR ")
      args = search_techs.collect {|t| like(t)}
      _projects = _projects.where [sql, *args]
    end
    if search_business_div.present?
      _projects = _projects.where ['business_div LIKE ?', like(search_business_div)]
    end
    if search_project_owner.present?
      _projects = _projects.where ['project_owner LIKE ?', like(search_project_owner)]
    end
    if search_status.present?
      _projects = _projects.where ['status LIKE ?', like(search_status)]
    end
    if search_keywords.present?
      _projects = _projects.where ['keywords LIKE ?', like(search_keywords)]
    end
    # now you have applied only the present scopes. return the result, and watch 
    # the query as it executes.
    _projects
  end