我有一个搜索字段,它从两个不同的表中查询三个不同的列:职位表中的“公司”和“行业”以及教育表中的“学校”。它成功返回所有满足输入搜索字段的所有字段的用户(使用 select_tag)。这是我的观点:
<%= form_tag(search_path, :method => :get, :id => "people_search") do %>
<div class="row-fluid">
<div class="span4">
<table>
<tr>
<td>
<div class="search-table" style="padding-left:55px">
<%= select_tag "all", options_for_select((@visible_companies + @visible_industries + @visible_schools).uniq, params[:all]), { :placeholder => "Search by companies, industries and schools...", :multiple => true, :js => "if (evt.keyCode == 13) {form.submit();}" } %>
</div>
</td>
<td>
<%= submit_tag "Add", id: "send-button", style:"width:175px;" %>
</td>
</tr>
</table>
</div>
<% end %>
</div>
和控制器:
@visible_positions = Position.where{ is_visible.eq('true') }
@visible_educations = Education.where{ is_visible.eq('true') }
@visible_companies = @visible_positions.order("LOWER(company)").map(&:company).uniq
@visible_industries = @visible_positions.order("LOWER(industry)").map(&:industry).uniq
@visible_schools = @visible_educations.order("LOWER(school)").map(&:school).uniq
@c = @visible_positions.where{company.in(my{params[:all]})}.map(&:user_id)
@i = @visible_positions.where{industry.in(my{params[:all]})}.map(&:user_id)
@s = @visible_educations.where{school.in(my{params[:all]})}.map(&:user_id)
@C = @visible_positions.where{company.in(my{params[:all]})}.map(&:company)
@I = @visible_positions.where{industry.in(my{params[:all]})}.map(&:industry)
@S = @visible_educations.where{school.in(my{params[:all]})}.map(&:school)
@blacklist = []
@cis = @c + @i + @s
@experiences = ([@C,@I,@S].reject(&:empty?).reduce(:&))
@cis.uniq.each do |user_id|
unless @C.empty?
@C.uniq.each do |company|
unless Position.find_all_by_company(company).map(&:user_id).include?(user_id) || Position.find_all_by_industry(company).map(&:user_id).include?(user_id) || Education.find_all_by_school(company).map(&:user_id).include?(user_id)
@blacklist << user_id
end
end
end
unless @I.empty?
@I.uniq.each do |industry|
unless Position.find_all_by_industry(industry).map(&:user_id).include?(user_id) || Position.find_all_by_company(industry).map(&:user_id).include?(user_id) || Education.find_all_by_school(industry).map(&:user_id).include?(user_id)
@blacklist << user_id
end
end
end
unless @S.empty?
@S.each do |school|
unless Education.find_all_by_school(school).map(&:user_id).include?(user_id) || Position.find_all_by_company(school).map(&:user_id).include?(user_id) || Position.find_all_by_industry(school).map(&:user_id).include?(user_id)
@blacklist << user_id
end
end
end
end
unless @c.empty? && @i.empty? && @s.empty?
@users = User.find(@cis - @blacklist)
end
搜索看起来像这样(注意单个字段),包括一个示例查询(注意 AND 过滤器...我是数据库中唯一适合所有搜索词的用户 ['dartmouth College' for school,'world health组织”代表公司,“互联网”代表行业]):
我意识到这不是一个有效的查询,并且正在考虑加快速度的方法,但此时可以使用一些想法。
火鸡节快乐 :)