Fellow Stranger
Fellow Stranger

Reputation: 34053

Reading deeply nested collection structure

How could I get a bird's view of a returned collection regarding structure and collection types? I find it hard to iterate through a collection with so many objects that I cannot see the structure of the nesting.

When executing Post.all.group_by {|post| post.category} a nested Hash of some sort seem to be returned. Post belongs_to Category.

Upvotes: 0

Views: 61

Answers (2)

Ruby Racer
Ruby Racer

Reputation: 5740

If you want to use iteration all you have to do is this:

Category.all.each do |category|
    category.posts.each do |post|
        # your logic
    end
end

Upvotes: 1

DevDude
DevDude

Reputation: 385

You can use the awesome_print gem for great formatting.

https://github.com/michaeldv/awesome_print

Upvotes: 4

Related Questions