I’ve managed (through this forumn) to search across multiple models with sunspot. Now I’m stuck on how to display the results in the way intended.
Here is what is in my controller
@search = Sunspot.search [Gear, User] do
fulltext params[:search]
paginate(page: params[:page])
end
@gears = @search.results
So before I added the User model I’d iterate over each Gear object as follows:
<% @gears.each do |gear| %>
<%= link_to gear_path(gear) do %>
<div class="gear_list_box">
<div class="gear_list_box_top"><%= image_tag gear.image_url(:list), class: 'gear_list_box_top_pic' %></div>
<div class="gear_list_box_bottom">
<table style="border-collapse:collapse; cellspacing: 0; width: 100%; margin: 0;" >
<tr>
<td style="color: #2c6aa7; padding-left: 10px" width="60%"> <span class='gearlist_size'><%= gear.size %></span> - <%= truncate(gear.title,:length => 12) %></td>
<td rowspan="2" width="40%" style="color: #5c8d0d; text-align: center; vertical-align: middle; font-size: 120%; border-collapse:collapse;">$<%= gear.price %>/day</td>
</tr>
<tr>
<td width="60%" style="padding-left: 10px; font-size: 12px;"><%= gear.user.first_name + " " + gear.user.last_name%></td>
</tr>
</table>
</div>
</div>
<% end %>
<% end %>
Now it throughs an error similar to the following:
NoMethodError in Gears#index
Showing /Users/dave/rails_projects/outdoor/app/views/gears/_gear.html.erb where line #3 raised:
undefined method `image_url' for #<User:0x007fd6654cae08>
since the User object obviously doesn’t have the same columns as the Gear object. How can I display this properly and still search by User Name?
The models are Gear:belongs_to User:has_many
Thank You.
I would preferably do something like this:
and in the Gear model: