Rob Rob - 5 months ago 33
Ruby Question

undefined method `value' for #<CategoryItemValue::ActiveRecord_Associations_CollectionProxy:0x007ff9706d24c0>

This is so simple but isnt working. What am I missing?

controlelr

@guide = Guide.friendly.find(params[:guide_id])
@category = @guide.categories.friendly.find params[:id]
@items = @category.category_items


view

<% @items.each do |item| %>
<%= item.category_item_values.value %>
<% end %>


gives the no method error of

undefined method 'value' for #<CategoryItemValue::ActiveRecord_Associations_CollectionProxy:0x007ff9706d24c0>


There is a
values
column in the
category_item_values
table so I'm not sure what the problem is.

Answer

item.category_item_values is the CollectionProxy instance (one might think of it as of an kinda array.)

Each category_item has [likely, you did not provide sufficiently enough info to guess more precisely] many values. If the assumption above is correct, here you go:

<% @items.each do |item| %>
  <% item.category_item_values.each do |value| %>
    <%= value %> # or maybe (depending on your model) <%= value.value %>
  <% end %>
<% end %>
Comments