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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:53:12+00:00 2026-05-26T18:53:12+00:00

I have a model object which is linked to a list of countries in

  • 0

I have a model object which is linked to a list of countries in a many-to-many relationship. The countries are keyed by their ISO 3166 alpha-2 codes.

The thing I’d like to accomplish is to save to use of a Country class and table, and only have my object have an accessor in the form of object.countries that will return an array of strings, e.g: ["IL", "US", "IT", ... ].

Essentially, a sort of has_and_belongs_to_many action with only the join table.
Is this possible?

Is there a best practice in the case of country lists?

  • 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-26T18:53:12+00:00Added an answer on May 26, 2026 at 6:53 pm

    This is how I eventually solved it, I created a countries table in the database, as I preferred a HABTM relationship.

    But to simplify things, I overrode the << operator on the collection so I could just push country codes as string or array of strings (object.country << 'IL'):

    has_and_belongs_to_many(:countries) do
      def <<(country)
        if [*country].all? {|c| c.is_a?(String) }
    
          countries = Country.where(:code => country)
          concat(*countries)
    
        else
    
          concat(country)
    
        end
      end
    end
    

    You might recognize that [*...] trick from here.
    Passing concat(*countries) also makes Rails insert all the rows in a single transaction, which is a nice bonus.

    And added a function to return a simplified hash of codes to countries:

    def countries_hash
      return self.countries.map {|c| { c.code => c.name }}.inject({}) {|hash, elem| hash.merge!(elem)}
    end
    

    Of course I’d be glad to hear any suggestions.

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

Sidebar

Related Questions

Say I have a model object 'Person' defined, which has a field called 'Name'.
I have a project which exposes object model to use by different types of
I have a fairly involved managed data model which has a central object with
I have a an object stored in the model called domain, which has two
I have an Address object in my model which has 6 address lines as
I have a multi step form which uses one model object and I need
I have a model named 'Object' which loads the 'Objects' table I have a
I have a hierarchy of model objects which I will be displaying on different
I have an object which contains models for my ASP.NET MVC web app. The
Okay here is what I'm trying to do: I have a model object that

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.