我正在尝试构建一个搜索功能,用户可以在其中搜索不同的字段,例如。使用下拉菜单的客户、行业、技术。搜索功能一直有效,直到我决定允许用户一次搜索不止一种技术。
这是我的模型:
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