I have an array that contains objects from two models:
@search_results = User.find(:all, :conditions => ['name LIKE ?', "%#{params[:query]}%"])
@search_results += Book.find(:all, :conditions => ['title LIKE ?', "%#{params[:query]}%"])
I then tried to parse them out like so:
<% @search_results.each do |result| %>
<% if result.title %>
<%= link_to result.title, result %>
<% else %>
<%= link_to result.name, result %>
<% end %>
<% end %>
I had hoped that the if statement would parse books (which have a title) from users (which do not). Unfortunately, the if statement itself throws the error “Undefined method `title’ for #”. What else can I do to determine the model an object belongs to?
PS. I want to keep both models in the same array so I can rank the results by a shared attribute, page_views.
You want
Reference : How do I get the name of a Ruby class?