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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:47:33+00:00 2026-05-18T01:47:33+00:00

I have an app with country preferences. A user will have 2 types of

  • 0

I have an app with country preferences. A user will have 2 types of country preferences – event and research. In the future there may be more. I was leaning more towards having 2 tables to represent this over using STI. I’m having a bit of trouble configuring Rails elegantly to do this. I could hack it but I would rather do this by Rails convention. What I want is something like this:

class User < ActiveRecord::Base
  has_many event_countries, :through => :event_countries, :class_name => 'Country'
  has_many research_countries, :through => :research_countries, :class_name => 'Country'
end

class EventCountry < ActiveRecord::Base
  belongs_to :country
  belongs_to :user
end

class ResearchCountry < ActiveRecord::Base
  belongs_to :country
  belongs_to :user
end

class Country < ActiveRecord::Base
...
end

This doesn’t work though. Given this “pseudo code” does anyone know how to actually implement this in Rails?

  • 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-18T01:47:34+00:00Added an answer on May 18, 2026 at 1:47 am

    I think you’re going about declaring them wrong, because this should work properly. That’s what the :through directive is for:

    class User < ActiveRecord::Base
      has_many :event_countries
      has_many :countries_with_events,
        :through => :event_countries,
        :source => :country
    
      has_many :research_countries
      has_many :countries_with_researches,
        :through => :research_countries,
        :source => :country
    end
    
    class EventCountry < ActiveRecord::Base
      belongs_to :country
      belongs_to :user
    end
    
    class ResearchCountry < ActiveRecord::Base
      belongs_to :country
      belongs_to :user
    end
    
    class Country < ActiveRecord::Base
      # ...
    end
    

    A lot of the awkwardness comes from the labels you’ve chosen for the tables. Although they’d seem reasonable at first glance, the way you use them ends up making them difficult.

    You might want to call research_countries something like user_research_countries so that the relationship name can be user.research_countries as the :through:

    class User < ActiveRecord::Base
      has_many :user_event_countries
      has_many :event_countries,
        :through => :user_event_countries,
        :source => :country
    
      has_many :user_research_countries
      has_many :research_countries,
        :through => :user_research_countries,
        :source => :country
    end
    
    class UserEventCountry < ActiveRecord::Base
      belongs_to :country
      belongs_to :user
    end
    
    class UserResearchCountry < ActiveRecord::Base
      belongs_to :country
      belongs_to :user
    end
    
    class Country < ActiveRecord::Base
      # ...
    end
    

    You can refactor this even further by adding a field to the user-country association table that includes one or more flags, which in this case would be research or event or whatever you require later:

    class User < ActiveRecord::Base
      has_many :user_countries
      has_many :event_countries,
        :through => :user_countries,
        :source => :country,
        :conditions => { :event => true }
      has_many :research_countries,
        :through => :user_countries,
        :source => :country,
        :conditions => { :research => true }
    end
    
    class UserCountry < ActiveRecord::Base
      belongs_to :country
      belongs_to :user
    
      # * column :event, :boolean
      # * column :research, :boolean
    end
    
    class Country < ActiveRecord::Base
      # ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my app that have some tab's (tabbar). In the first tab, there
hundreds of our clients around the country have a vb6/MS Access app. The boss
This is the scenario i have: im developing a web app that will list
We’re developing an iOS app. One requirement we have is, get the country of
Is there a way to programmatically find out what country a user's phone is
I have a few timepickers in my app. In my country (Spain) we are
I have an iPhone app. The user can select a contact. Upon selection another
I have a menu that let's a user select a country. Exactly like that
I have app.js and routes.js . In app.js I require routes.js . For routes.js
I have app.config in m win application, and loggingConfiguration section (enterprise library 4.1). 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.