Rails搜索多个模型 [英] Rails Search Across Multiple Models
问题描述
谢谢
公司/ 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屋!