Ruby on Rails:一次搜索即可搜索两个模型 [英] Ruby on Rails: Searching two models in one search

查看:69
本文介绍了Ruby on Rails:一次搜索即可搜索两个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a search page where a user can search for different projects, using different dropdown menus. They can search for more than one field at a time, and the search will return something if it finds a project that matches all fields.

在我的项目控制器中执行我的搜索操作:

Heres my search action in my project controller:

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

这是我的搜索视图.

<%= stylesheet_link_tag "search" %>
<body>
<div id = "search_op">

<%= form_tag search_path, method: :get do %>


<div class="client">
Client : 
<%= select(@projects, :client, Project.order("client").map{|p| [p.client]}.uniq, :prompt => "-Any-", :selected => params[:client]) %></br>
</div>

<div class="industry">
Industry :
<%= select(@projects, :industry, Project.order("industry").map {|p| [p.industry]}.uniq, :prompt => "-Any-", :selected => params[:industry]) %></br>
</div>


<div class="tech">
Technologies :
<%#= select(@projects, :tech, Project.order("tech").map {|p| [p.tech]}.uniq, :prompt => "-Any-", :selected => params[:tech]) %></br>
<!/div>

<%= fields_for(@project_technol) do |ab| %>



<%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple => true } ) %>
</div>
<% end %>



<div class="keywords">
Keywords :

<%= text_field_tag :keywords, params[:keywords] %></br>
</div>




<div class="results">
Results per page: <%= select_tag :per_page, options_for_select([10,20,50], :selected=>params[:per_page]),  { :onchange => "this.form.submit();"}   %></br>


<%#Results per page: <%= select_tag :per_page, options_for_select([1,2,5], :selected=>params[:per_page]),  :onchange => "'#{request.query_string}&per_page=' + this.getValue()"  %></br>
</div>

<div class="buttons">
<div id="search_button">
<%= submit_tag "Search", name: nil, :class => "button" %>
</div>

<% end %>

<div class="home_button">
<%= button_to "Home", projects_path, :class => "button", :method => "get"  %>
</div>



<div id="reset_button">
<%= button_to "Reset Search", search_path, :class => "button", :method => "get" %>
</div>


</div>
</div> <%#search_op div%>







<div class ="menu"></div>



<div id ="section3">

<% if @project_search.total_entries > 0 %>
<% if @search_performed %>


<table id = "mytable">

  <tr>
    <th><%= sortable "project_name", "Project name" %> </th>
    <th><%= sortable "client", "Client" %></th>
    <th><%= sortable "tech", "Technologies" %></th>
    <th><%= sortable "industry", "Industry" %></th>

  </tr>



<% @project_search.each do |t| %>
  <tr>
    <td><%= link_to t.project_name, t %></td>
    <td><%= t.client %></td>

    <td><%= t.tech %></td>
    <td><%= t.industry %></td>

    <!td><%#= link_to 'Show', t %></td>
    <!td><%#= link_to 'Edit', edit_project_path(project) %></td>
    <!td><%#= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table></br>

<div id ="total_results"><%=@project_search.total_entries%> results</div>
<%end %>



<% else %>
<h2> Sorry, there are no results matching your search. Please try again. </h2> 
<% end %>
<br />
<%# end %>


</div> <%# table div %>
</div> <%# section2 div %>



<% if @search_performed %>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<%= hidden_field_tag :per_page, params[:per_page] %>
<%= hidden_field_tag :page, params[:page] %>


<%= will_paginate (@project_search), :class => 'will' %>

<% end %>



</body>


</html> 

这是我的project.rb:

and here is my project.rb:

class Project < ActiveRecord::Base


has_many :projecttechnols
has_many :technols, :through => :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

如果我使用调试,并在技术下拉列表中搜索第一项技术,则会得到此提示.

If I use debug, and search for the first technology in the technology drop down, I get this.

---
utf8: ✓
client: ''
industry: ''
technols:
  id:
  - ''
  - '1'

keywords: ''
per_page: '10'
action: search
controller: projects
page: 1

但没有结果.

我有一个新表,其中包含与项目相关的所有技术,但是我在设置搜索,搜索所有字段以及搜索是否有与之匹配的技术时遇到了麻烦在Technol表中.

I have a new table that holds all the technologies that are related to a project, and I am having trouble setting up the search, to search on all fields, as well as searching to see if any of the technologies match with the ones in the Technol table.

谁能指出我正确的方向.我是Rails的新手,所以在寻求帮助时请记住这一点.

Can anyone point me in the right direction. I am new to rails, so please remember this when trying to help.

谢谢.

推荐答案

或尝试使用外部搜索引擎(例如Sphinx和think_sphinx gem)

Or try using external search engine like Sphinx, and thinking_sphinx gem

这篇关于Ruby on Rails:一次搜索即可搜索两个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆