indus
indus

Reputation: 103

How to get model name out of array

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.

Upvotes: 1

Views: 134

Answers (2)

anil
anil

Reputation: 159

Try This:

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

Upvotes: 3

byakugie
byakugie

Reputation: 653

Try This :

<% if s.class.to_s == "Foo" %>

Upvotes: 5

Related Questions