5

I got four different models.

Here's an example,

@single = Single.all @coe = Coe.all @blend = Blend.all @production = @single+@coe+@blend 

then how to check which model @production is?

I tried

<% @production.each do |p| %> <%=p.class.name%> <% end %> 

but it returns "Array"

It seems to be simple, but I can't find out (I edited question)

2
  • can you share the output of @production and @production.class? Commented Jul 20, 2016 at 6:03
  • It returns "Array" Commented Jul 20, 2016 at 6:17

3 Answers 3

13

The problem is here

@single = Single.all @coe = Coe.all @blend = Blend.all @production = @single+@coe+@blend 

change these lines with

@single = Single.all.to_a @coe = Coe.all.to_a @blend = Blend.all.to_a @production = @single+@coe+@blend 

and then if you will check

@production.first.class.name #Single @production.last.class.name #Blend 

so in your view you can do this

<% @production.each do |p| %> <% p.each do |product| %> <%= product.class.name %> <% end %> <% end %> 
Sign up to request clarification or add additional context in comments.

1 Comment

changed Object.all to Object.all.to_a worked! thanks a lot!!
2

if while iterating on @production it returns array so you need to try this.

<% @production.each do |p| %> <% p.each do |product| %> <%= product.class.name %> <% end %> <% end %> 

Comments

1

@production is a collections of combinations of single, coe, and blend thats why @production.class.name doesnt work, you need to iterate each object like this:

<% @production.each do |object| %> <%= object.class.name %> <% end %> 

1 Comment

does objects exists for @single = Single.all, @coe = Coe.all, @blend = Blend.all ? if not then results will be NilClass

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.