Rails搜索多个模型 [英] Rails Search Across Multiple Models

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

问题描述

我有一个问题。我有一个显示视图,充当仪表板并引入其他模型的记录,然后引入与之相关的模型。我有一个简单的搜索表单,可以很好地搜索一个模型,但我不知道如何通过它的关联模型来查看它。我不认为全文搜索是必要的,我不确定它是如何工作的,因为我不想要在整个网站上搜索的东西。

谢谢

公司/ show / 1

 < div id =form> 
< div class =search>
<%form_tag battalion_company_path,:method => :get do%>
< p>
<%= text_field_tag:search,params [:search]%>

<%= submit_tagSearch,:name =>无%>

< / p>
<%end%>
< / div>
< / div>

< div id =bc_box>
<%@ soldiers.each do | soldier | %GT;
< div id =bc_focus>
< div class =right>
<%= link_to image_tag(../ images / buttons / info.png,:border => 0),battalion_company_soldier_path(@battalion,@ company,soldier)%>
<%= link_to image_tag(../ images / buttons / edit.png,:border => 0),edit_battalion_company_soldier_path(@battalion,@ company,soldier)%>
< / div>
<%= h soldier.rank%> <%= h soldier.lastname%>< br />
手机:<%= h soldier.cellphone%>< br />
<%soldier.primaries.each do | primary | %GT;
< p>
<%= h primary.firstname%> <%= h primary.lastname%> (<%= h primary.relationship%>)< br />

(c):<%= h primary.cellphone%>< br />
<%= h primary.email%>< br />
< / p>
<%end%>

< / div>
<%end%>
< / div>

soldier.rb

  def self.search(搜索)
如果搜索
find(:all,:conditions => ['email LIKE?OR lastname LIKE?or firstname LIKE?', #{search}%,%#{search}%,%#{search}%])
else
find(:all,:order =>'lastname')
end
end

companies_controller

  @soldiers = @ company.soldiers.search(params [:search])
@primary = @ company.primaries.find(:all,:conditions => ; [relationship ='Spouse'])


解决方案

你的应用程序后面的搜索引擎如何思维狮身人面像?将缓存和搜索工作留给其他人,并让你的Rails应用程序提供结果。


I have an issue. I have a show view that acts as a dashboard and brings in records from other models and then models associated to that. I have a simple search form that is working fine to search through one model, but I don't know how to have it look through its associated models as well. I don't think a full text search is necessary and I am not sure how it would work because I don't want something that is going to search across the whole site.

Thanks

companies/show/1

<div id="form">
  <div class="search">
   <% form_tag battalion_company_path, :method => :get do %>
   <p>
   <%= text_field_tag :search, params[:search] %>

   <%= submit_tag "Search", :name => nil %>

   </p>
 <% end %>
</div>
</div>

<div id="bc_box">
<% @soldiers.each do |soldier| %>
<div id="bc_focus">
   <div class="right">
    <%= link_to image_tag("../images/buttons/info.png", :border=>0),  battalion_company_soldier_path(@battalion, @company,soldier) %>
    <%= link_to image_tag("../images/buttons/edit.png", :border=>0), edit_battalion_company_soldier_path(@battalion, @company,soldier) %>
    </div>
   <%=h soldier.rank %> <%=h soldier.lastname %><br />
   Cell: <%=h soldier.cellphone %><br />
   <% soldier.primaries.each do |primary| %>
   <p>
   <%=h primary.firstname %> <%=h primary.lastname %> (<%=h primary.relationship %>)<br />

   (c):<%=h primary.cellphone %><br />
   <%=h primary.email %><br />
   </p>
    <% end %>

</div>
  <% end %>
</div>

soldier.rb

def self.search(search)
  if search
    find(:all, :conditions => ['email LIKE ? OR lastname LIKE ? OR firstname LIKE ?', "%#{search}%", "%#{search}%", "%#{search}%"])
  else
    find(:all, :order => 'lastname')
  end
end

companies_controller

@soldiers = @company.soldiers.search(params[:search])
@primary = @company.primaries.find(:all,:conditions => ["relationship = 'Spouse'"])

解决方案

How about a search engine behind your app such as Thinking Sphinx ? Leave the hard work of caching and searching to someone else and let your Rails app serve the result.

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

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