1

I have a search controller something like this:

def index @foos = Foo.search @bars = Bar.search @search = [@foos, @bars] end 

And in my search index:

<% @search.each do |s| %> <% s.each do |s| %> <% s.name %> <% end %> <% end %> 

How can I write an if statement to find arrays only from the Foo model?

<% if s.modelname == "Foo" %> ? 

Setting <%= s.each do %> shows that the array does have it's model name.

2 Answers 2

5

Try This :

<% if s.class.to_s == "Foo" %> 
Sign up to request clarification or add additional context in comments.

1 Comment

Why the to_s stuff when you can say s.class == Foo or better s.is_a? Foo instead?
3

Try This:

<% @search.each do |s| %> <% if s[0].class.to_s == "Foo" %> <% s.each do |s| %> <% s.name %> <% end %> <% end %> 

1 Comment

With that we are checking the condition before entering into the sub loop ,So It will reduces the loop iterations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.