Ruby on Rails:在一次搜索中搜索两个模型

我有一个搜索页面,用户可以使用不同的下拉菜单搜索不同的项目。 他们可以一次搜索多个字段,如果找到与所有字段匹配的项目,搜索将返回一些内容。

inheritance我的项目控制器中的搜索操作:

def search @search = params[:client], params[:industry], params[:tech], 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]) @all_technols = Technol.all @project_technol = @project.projecttechnols.build respond_to do |format| format.html # search.html.erb format.json { render :json => @project } end end 

这是我的搜索视图。

   
Client : "-Any-", :selected => params[:client]) %>
Industry : "-Any-", :selected => params[:industry]) %>
Technologies : "-Any-", :selected => params[:tech]) %>
true } ) %>
Keywords :
Results per page: params[:per_page]), { :onchange => "this.form.submit();"} %>
<%#Results per page: params[:per_page]), :onchange => "'#{request.query_string}&per_page=' + this.getValue()" %>
"button" %>
"button", :method => "get" %>
"button", :method => "get" %>
0 %>

results

Sorry, there are no results matching your search. Please try again.


'will' %>

这是我的project.rb:

 class Project  :projecttechnols def self.search(search_client, search_industry, search_tech, search_keywords) return scoped unless search_client.present? || search_industry.present? || search_tech.present? || search_keywords.present? where(['client LIKE ? AND industry LIKE ? AND tech LIKE ? keywords LIKE ?', "%#{search_client}%", "%#{search_industry}%" , " "%#{search_tech}%" "%#{search_keywords}%" ]) end def self.paginated_for_index(projects_per_page, current_page) paginate(:per_page => projects_per_page, :page => current_page) end end 

如果我使用调试,并在技术下拉中搜索第一项技术,我会得到这个。

 --- utf8: ✓ client: '' industry: '' technols: id: - '' - '1' keywords: '' per_page: '10' action: search controller: projects page: 1 

但没有结果。

我有一个新表,其中包含与项目相关的所有技术,我无法设置搜索,搜索所有字段,以及搜索是否有任何技术与Technol表。

任何人都可以指出我正确的方向。 我是rails的新手,所以在尝试帮助时请记住这一点。

提前致谢。

也许你会尝试使用搜索gem,比如meta_search ?

或者尝试使用像Sphinx和thinking_sphinx gem这样的外部搜索引擎