0

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.

1 Answer 1

3

You want

result.class.name 

Reference : How do I get the name of a Ruby class?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.