【问题标题】:Ruby on Rails: Dropdown search - invalid input syntax for type boolean: "%%"Ruby on Rails:下拉搜索 - 布尔类型的无效输入语法:“%%”
【发布时间】:2012-10-01 14:02:58
【问题描述】:

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

这是我的模型:

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

【问题讨论】:

  • 糟糕,糟糕,糟糕……啧,啧,啧::tech2, :tech3, :tech4, :tech5techprojects 之间存在多对多关系。这样建模,你的想法应该会清晰。
  • 我尝试过。我问了一个问题,但我发现很难解释我想要做什么。 stackoverflow.com/questions/12636413/… 我认为将用户限制为 5 个技术菜单会更容易。我对 Rails 很陌生。
  • 你不应该在查询中包含一个变量,除非它有一个值,或者你应该提供一个默认值,所以 sql 是有效的。
  • 他们建议您使用现有的搜索解决方案之一,这将是一个非常好的主意。您正在尝试重新发明轮子。

标签: ruby-on-rails ruby postgresql syntax


【解决方案1】:

根据 sql 语法,您的查询无效。

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

试试这个:

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

【讨论】:

  • 这只是在表格的第一个技术列上搜索。还有其他想法吗?
【解决方案2】:

你的搜索方法可以写得更好,它会帮助你找到问题(基本上取决于无用的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

【讨论】:

  • 感谢您的帮助。当我使用这种技术时,我得到这个错误:undefined local variable or method _project for #&lt;Class:0xb643484&gt;
  • 抱歉,方法的最后一行有错字。在变量名中添加“s”。
  • 现在看来可以了。谢谢你。你能建议我如何摆脱技术列中的空白条目吗?我的“创建新项目”页面中有 5 个下拉菜单,如果用户未选择 5 项技术,则空的下拉菜单将提交为空白,现在显示在技术搜索下拉菜单中
猜你喜欢
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 2014-10-20
  • 2013-06-28
  • 2010-11-28
相关资源
最近更新 更多