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

  • Home
  • SEARCH
  • 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 7856957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:52:37+00:00 2026-06-02T20:52:37+00:00

I have a current City and Country model like this # City model city:string

  • 0

I have a current City and Country model like this

# City model
city:string
country_code:string

# Country model
country:string
country_code:string

I’m trying to create an association between both models using the country_code as foreign_key instead of using default’s country_id.

# city.rb
belongs_to :country, :foreign_key => "country_code"

# country.rb
set_primary_key :country_code
has_many :cities, :foreign_key => "country_code"

This query is not working

ruby-1.9.2-p290 :016 > Country.where(:country_code => "uy").cities
NoMethodError:   Country Load (0.2ms)  SELECT "countries".* FROM "countries" WHERE     "countries"."country_code" = 'uy'
undefined method `cities' for #<ActiveRecord::Relation:0x007f8e92ca0df0>
from /Users/pel/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.2.3/lib/active_record/relation/delegation.rb:45:in `method_missing'
from (irb):16
    from /Users/pel/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/pel/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/pel/.rvm/gems/ruby-1.9.2-p290/gems/railties-    3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
  • 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-06-02T20:52:38+00:00Added an answer on June 2, 2026 at 8:52 pm

    Try this:

    Country.where(:country_code => "uy").collect(&:cities)
    

    What you are missing:

    Check this error:

     undefined method `cities' for #<ActiveRecord::Relation:0x007f8e92ca0df0>
    

    where condition returns an array of objects. For example (Considering 1 id is present, in some cases)

     Country.first // will return an object or nil
    
     Country.find(1) // will return an object or will throw an exception
    
     Country.where(:id=> 1) // will return an array of only one object with id 1
    

    So

      Country.find(1) IS NOT EQUAL TO Country.where(:id=> 1)
    

    But

     Country.find(1) IS EQUAL TO Country.where(:id=> 1).first
    

    Thats about the basics. So the issue was

    Country.where(:country_code => "uy") 
    

    will return an array and then you are trying to apply “cities” mapping to that array which really don’t exists. So what you need to do is “collect” that will loop over all the objects in that array and then find out cities for each of these “country” objects and return an another array of array.

    One more thing to notice:

     Country.where(:country_code => "uy") 
    

    will return an array something like this:

    ['a','b','c','d']
    

    but

    Country.where(:country_code => "uy").collect(&:cities)
    

    will return an array like:

      [[1,2,3,4],[2,3,6,8],[8],[10]] ie an array of an array.
    

    So you need to flatten it like this:

      Country.where(:country_code => "uy").collect(&:cities).flatten
    

    Then if you need to need unique cities(if needed, based upon the records, which i don’t think you need for this case), you can add .uniq to the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User entity that has a Current Location field (city and country).
I have following code return (EseshEntities.Current.Users.Select(u => new { Comunity = u.Apartment.Building.District.City })).ToList(); if
I have three current thoughts on how to do this: re-implement AuthenticationService, which uses
As a beginner in Ocaml, I have this current working code: ... let ch_in
Can anyone help me getting started with this? We have a current keygen for
I have some sample data like so: MADISON COUNTY,,,,,,,,,,,,, London, City of,,,,,,,,,,,,597,519 2.1,mill /s,(replacement),for,5
I my iPhone application I have to find the city and country name of
I have done this function, using geocoder module, to get the city from a
I have a City model and a Business model. Business belogs_to :city Now I
I have a Place model that has both 'city_name' and 'name' as attributes. I

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.