group_by in views

63 views
Skip to first unread message

fugee ohu

unread,
Nov 21, 2015, 4:24:51 PM11/21/15
to Ruby on Rails: Talk
<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> 

Colin Law

unread,
Nov 21, 2015, 4:51:19 PM11/21/15
to Ruby on Rails: Talk
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

>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/0fe07e54-776d-45a3-812d-5aa6247d21dc%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Arvind Vyas

unread,
Nov 22, 2015, 1:22:17 AM11/22/15
to rubyonra...@googlegroups.com
category object is coming as an array so first check that 

Thanks and Regards,

Arvind Vyas
Ruby On Rails Developer
Mob ; 8511289155

--

fugee ohu

unread,
Nov 22, 2015, 2:24:18 AM11/22/15
to Ruby on Rails: Talk
so how do i dereference the array in the view to display the name

Arvind Vyas

unread,
Nov 22, 2015, 3:44:22 AM11/22/15
to rubyonra...@googlegroups.com
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

Thanks and Regards,

Arvind Vyas
Ruby On Rails Developer
Mob ; 8511289155

fugee ohu

unread,
Nov 22, 2015, 7:50:00 AM11/22/15
to Ruby on Rails: Talk
although the object's not null, the syntax you gave me "category.first.name" doesn't work "undefined method `name' for 0:Fixnum"

Colin Law

unread,
Nov 22, 2015, 8:24:24 AM11/22/15
to Ruby on Rails: Talk
On 22 November 2015 at 12:49, fugee ohu <fuge...@gmail.com> wrote:
> 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
> https://groups.google.com/d/msgid/rubyonrails-talk/845c3185-d24d-4163-bcd6-e59cc9f2c15c%40googlegroups.com.

Tamara Temple

unread,
Nov 22, 2015, 9:32:24 AM11/22/15
to rubyonra...@googlegroups.com

Arvind Vyas <arvind...@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 ..]}`

http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-group_by

>
> Thanks and Regards,
>
> Arvind Vyas
> Ruby On Rails Developer
> Mob ; 8511289155
>
> On Sun, Nov 22, 2015 at 2:54 AM, fugee ohu <fuge...@gmail.com> wrote:
>
>> <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>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby on Rails: Talk" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to rubyonrails-ta...@googlegroups.com.
>> To post to this group, send email to rubyonra...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/rubyonrails-talk/0fe07e54-776d-45a3-812d-5aa6247d21dc%40googlegroups.com
>> <https://groups.google.com/d/msgid/rubyonrails-talk/0fe07e54-776d-45a3-812d-5aa6247d21dc%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>

--
Tamara Temple
tam...@gmail.com
http://www.tamouse.org

Allen Maxwell

unread,
Nov 22, 2015, 10:44:40 AM11/22/15
to Ruby on Rails: Talk
This isn't related to your issue BUT, you shouldn't have <%=  on the do line... just use <%



<h2>Categories</h2>

<% @categories.group_by(&:parent_id).each do |category| %>

<%= category.name %><br>

<% end %>

Allen Maxwell

unread,
Nov 22, 2015, 11:13:51 AM11/22/15
to Ruby on Rails: Talk
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:

<h2>Categories</h2>

<% @categories.group_by(&:parent_id).each do |parent_id, categories| %>

<%= <h3>parent_id</h3> %>

<% categories.each do |category| %>

<%=    category.name %><br/>

<%  end %>

<% end %>







On Saturday, November 21, 2015 at 2:24:51 PM UTC-7, fugee ohu wrote:

Allen Maxwell

unread,
Nov 23, 2015, 9:40:43 AM11/23/15
to Ruby on Rails: Talk
dumb mistake in my example... Move the <h3> tags out of the erb insert...

<h2>Categories</h2>

<% @categories.group_by(&:parent_id).each do |parent_id, categories| %>

<h3>

   <%= parent_id%>

</h3>

<% categories.each do |category| %>

<%=    category.name %><br/>

<%  end %>

<% end %>

Reply all
Reply to author
Forward
0 new messages