Reputation: 171
I would like to display whatever number of items has been found and returned to the user. In my database, there are 4 items. The search feature works fine. What I now want to display is whatever number of records has been found. If user searches "aI", 2 items gets returned and I want to display the text that 2 items has been found. I tried to do that in view --> layout --> application.html.erb.
Upvotes: 1
Views: 1060
Reputation: 1478
You have to count the actual results. Product (with a capital p) will always return all objects of that class in the database. Hence 26. Save the result in an ivar (@products) and call count on that in your view instead.
In Controller
@products = Product.fuzzy_search2(params[:search_string])
In View
<% if @products %>
<span> <%= @products.count %> Books Found</span>
<% end %>
Upvotes: 5
Reputation: 417
I'm assuming that you are storing the search results in an ivar. You need to call .count
on that ivar or .size
/ .length
if it's stored in an array. The issue is that when you are calling Product.count
you are getting the count of all products in your database always, because that's exactly what you are telling it to do.
Upvotes: 2