Nested Data

I currently have a database with a table called bookstores. Within that
table are columns such as bookstore name, city, state, and zip code.

What I’d like to do is display the City and then every store name for
that city underneath it.

i.e.

City

  • Store 1
  • Store 15

Another City

  • Store 2
  • Store 5

etc. I’ve seen act_as_tree but I don’t know if it’ll help me as I’m just
using one table here. Is there a rails way to do what I want?

Thanks!

I think this is probably what you’re looking for…

@bookstores_by_city = Bookstore.find_all.group_by { |store| store.city }

See http://www.ruby-doc.org/core-1.9/classes/Enumerable.html

-dan

Dan K. wrote:

I think this is probably what you’re looking for…

@bookstores_by_city = Bookstore.find_all.group_by { |store| store.city }

See http://www.ruby-doc.org/core-1.9/classes/Enumerable.html

-dan

Dan,

I just wanted to say thank you for your help. I’m learning more and more
everyday and its great to know there’s a big community that’s willing to
help you learn.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs