Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5842005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:57:43+00:00 2026-05-22T11:57:43+00:00

Sorry for this newbie question but I cannot figure how I can do this..

  • 0

Sorry for this newbie question but I cannot figure how I can do this..

I have the following City table

# Table name: cities
#
#  id          :integer
#  name        :string(255)
#  country     :string(255)

(note: I am not using a separate Country table as I use Geokit-rails and think it is simpler to store all Google queries in the same table. However It would be easier to have separate tables to render what I want through a belong_to/has_many association)

In my city/index view I want to loop all the cities for each countries in order to render something like:

United States
  New York
  San Francisco
  Los Angeles
United Kingdom
  London
Spain
  Madrid
...

By now, What I can get is what it provided by scaffolding

Model

def index
  @cities = City.all
end

View

<table>
  <% for city in @cities %>
  <tr>
    <th><%= city.country %></th>    
  </tr>
  <tr>
    <td><%= city.name %></td>
  </tr>
    <% end %>
</table>

Rendering:

United States
  Los Angeles
United States
  New York
...

I didn’t find any resource about this. I would be pleased if someone could help me (I don’t know with which I have to start: find_by_ /params /each or collection?)

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:57:43+00:00Added an answer on May 22, 2026 at 11:57 am

    The correct way to do this is to create a Country table with id and name and in your City model just keep a country_id to the country, instead of a string

    so

    class Country < ActiveRecord::Base
      has_many :cities
    end
    
    class City < ActiveRecord::Base
      belongs_to :country
    end
    

    then you can call Country.includes(:cities) and iterate over them like this in your view:

    <% for country in countries %>
      <%= country.name %>
      <% for city in country.cities %>
        <%= city.name %>
      <% end %>
    <% end %>
    

    If you still want to use your way, you could do something like

    @cities = City.order("country asc")
    @countries = @cities.map(&:country).uniq!
    
    <% for country in @countries %>
      <%= country.name %>
      <% for city in @cities %>
        <%= city.name if city.country == country %>
      <% end %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok sorry this might seem like a dumb question but I cannot figure this
Sorry if this sounds like a newbie question, but the other day a Java
Sorry if this is a newbie question. I have a VB.NET subroutine that is
Sorry for this lame question, but I really am a database newbie: I want
I'm sorry for this newbie question, but I need a quick answer to tell
I'm sorry if this is a newbie question, but it seems I don't get
Sorry this is a very newbie question. We have this massive application I am
I am sorry for the very newbie question, but this is driving me mad.
Sorry for the second newbie question, I'm a developer not a sysadmin so this
Sorry for this not being a real question, but Sometime back i remember seeing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.