Categories
<%= @categories.group_by(&:parent_id).each do |category| %> <%= category.name %><% end %>
This code results in ActionController returning
undefined method `name' for #<Array:0xb48bf628>
This code results in ActionController returning
undefined method `name' for #<Array:0xb48bf628>
<h2>Categories</h2> <%= @categories.group_by(&:parent_id).each do |category| %> <%= category.name %><br> <% end %>
This code results in ActionController returning
undefined method `name' for #<Array:0xb48bf628>
To see what is in category you can insert the line <% logger.info category.inspect %> before the category.name line. Then you can look in the log to see what is there. That may make things more clear.
Colin
category object is coming as an array so first check that
so how do i dereference the array in the view to display the name
As I told you in earlier check what object you are getting and fetch the name accordingly.
For example if I got category object is like [#<Category id: 1, name: “2012-08-31 20:34:15”>]
so in simplest way I can write category.first.name but in this case you have to check cateroy object should not nil.
Also you can check this example it has also same issue as you are looking form http://stackoverflow.com/questions/12124941/undefined-method-name-for-array-in-activeadmin-row
although the object’s not null, the syntax you gave me “category.first.name” doesn’t work "undefined method
name’ for 0:Fixnum`"
although the object's not null, the syntax you gave me "category.first.name" doesn't work "undefined method `name' for 0:Fixnum"
What did you see when you tried my suggestion?
Colin
Arvind Vyas <arvindvyas07@gmail.com> writes:
category object is coming as an array so first check that
With `group_by`, it's actually a hash of the counts for each grouping and the group collection. Thus each category is `{ 10 => [.. array of category objects ..]}`
This isn’t related to your issue BUT, you shouldn’t have <%= on the do line… just use <%
<% @categories.group_by(&:parent_id).each do |category| %>
<%= category.name %>
<% end %>
Doing it this way seems to return an array of arrays where the 0 index is the index and the 1 index is the list of items in that group…
try doing something like this:
<% @categories.group_by(&:parent_id).each do |parent_id, categories| %>
<%=
<% categories.each do |category| %>
<%= category.name %>
<% end %>
<% end %>
dumb mistake in my example… Move the
<% @categories.group_by(&:parent_id).each do |parent_id, categories| %>
<%= parent_id%>
<% categories.each do |category| %>
<%= category.name %>
<% end %>
<% end %>