我有一个ActiveRecord模型(个人资料), HABTM 与模型(主题)有关联。 我正在使用太阳黑子搜索基于两个字段的所有个人资料,用户将输入:zip 和(主题.title伪)
我在stackoverflow上尝试了很多不同的例子,但是我试图找到答案的尝试已经用尽了。我绝对是铁杆新手,我很感激任何帮助。谢谢。
个人资料模型
class Profile < ActiveRecord::Base
has_and_belongs_to_many :subjects
searchable do
string :zip
string :title do
subjects.title
end
end
ProfilesController#索引
def index
@search = Profile.search do
with(:zip, params[:zip])
# Don't know what to put here if this is even correct... ???
end
@profiles = @search.results
end
ProfilesView#索引
<%= form_tag profiles_path, method: :get do %>
<p>
<p>Enter Zip:</p>
<%= text_field_tag :zip, params[:search] %>
<p>Enter Subject:</p>
<%= text_field_tag :title, params[:search] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
<% for profile in @profiles %>
<ul style="list-style:none;">
<li> <%= link_to profile.name, profile %> </li>
<li> <%= profile.zip %></li>
<li><b>($<%= profile.rate %>)</b></li>
<% profile.subjects.each do |s| %>
<li> <%= s.title %> </li>
<% end %>
</ul>
<% end %>
答案 0 :(得分:1)
了解如何做到这一点......
个人资料模型
class Profile < ActiveRecord::Base
has_and_belongs_to_many :subjects
searchable do
text :subject_search
string :zip
end
def subject_search
subjects.map { |subject| subject.title }
end
end
<强>情景#控制器强>
def index
@search = Profile.search do
fulltext :params[:subject_search]
with(:zip, params[:zip])
end
@profiles = @search.results
end
<强>情景#索引强>
<%= form_tag profiles_path, method: :get do %>
<p>
<p>Enter Zip:</p>
<%= text_field_tag :zip, params[:search] %>
<p>Enter Subject:</p>
<%= text_field_tag :subject_search, params[:search] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>