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 9220381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:20:00+00:00 2026-06-18T03:20:00+00:00

Classes: class RefCountry < ActiveRecord::Base has_many :cities, :class_name => :RefCity, :foreign_key => country_id, :primary_key

  • 0

Classes:

class RefCountry < ActiveRecord::Base
  has_many :cities, :class_name => :RefCity, :foreign_key => "country_id", :primary_key => "country_id"
end

class RefCity < ActiveRecord::Base
  belongs_to :country, :class_name => :RefCountry, :primary_key => "country_id"
end

All links are working.

if I try to do

RefCity.joins(:country)

i get ActiveRecord::Relation

[
RefCity country_id: 1306, region_id: 4515, city_id: 67101, name_en: "Ust-Orda">, 

RefCity country_id: 1285, region_id: 4483, city_id: 67102, name_en: "Ust-Tarka", ...]

But, I just want to get the data type

{city_id1, city_name1, country_id1, country_name1},
{city_id2, city_name2, country_id2, country_name2},
...
{city_idN, city_nameN, country_idN, country_name}

for use in the ajax

  • 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-18T03:20:01+00:00Added an answer on June 18, 2026 at 3:20 am

    Try this out

     >> cities = RefCity.joins(:country).select('ref_cities.id, ref_cities.name, ref_cities.country_id, countries.name AS country_name')
     >> cities.first.id # city id
     >> cities.first.name # city name
     >> cities.first.country_id # country id
     >> cities.first.country_name # country name
     >> cities.map { |city| [city.id, city.name, city.country_id, city.country_name] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i have these classes: class Core < ActiveRecord::Base belongs_to :resource, :polymorphic => true
I have two classes: class Activity < ActiveRecord::Base belongs_to :activity_type def belongs_to_cat_a? self.activity_category ==
I have the following classes: class Product < ActiveRecord::Base has_one :total_purchase end class TotalPurchase
I have two ActiceRecord classes: class A < ActiveRecord::Base has_one :b end class B
I have the following classes: class Vigil < ActiveRecord::Base after_update :do_something_cool private def do_something_cool
Given the following classes: class Candidate has_many :applications has_many :companies, :through => :job_offers end
I have these simple classes Class A(models.Model): ... Class Meta(models.Model): a = models.OnetoOneField(A, primary_key=True)
Suppose we have two classes: class Base { private: int x; public: void f();
I have 4 base classes: class A { virtual SomeMethod () { <code> }
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived

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.