I’m trying to retrieve data for all items from a box, a box can have compartments and I’d like to get all compartment info at the box level. items are made polymorphic as boxes won’t necessarily have compartments.
MODEL
class Box < ActiveRecord::Base
has_many :compartments
has_many :items, :as => :itemable
end
In my Controller I can get results back with:
@box = Box.find(params[:id])
@itemable = @box.compartments.first
@itemable = @box.compartments.last
VIEW
<% @items.each do |item| %>
<%= item.name %>
<% end %>
but if I then try
@itemable = @box.compartments
OR
@itemable = @box.compartments.find(:all)
I get the error
undefined method `items' for #<ActiveRecord::Array>
OR
undefined method `items' for #<ActiveRecord::Relation>
Can anyone help with getting results back from all compartments?
So in compartments you have
Is this the case?
@box.compartmentsshould return an array of compartments right? It sounds likeitemsis somehow getting called on@box.compartmentssomehow