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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:33:22+00:00 2026-05-31T19:33:22+00:00

I have a User model and an Account model. The user has many accounts

  • 0

I have a User model and an Account model. The user has many accounts and the accounts belong to one user. I have the models and associations all set up. Now I want to make one of those accounts the “primary account”. What is the best way to set up the associations? I added a primary_account_id column to my user table and set up the associations like this but it didn’t work. Any tips?

class User < ActiveRecord::Base
   has_many :accounts
   has_one :primary_account, :class_name => "Account"
end

class Account < ActiveRecord::Base
   belongs_to :user
end

Edit

I see this question Rails model that has both 'has_one' and 'has_many' but with some contraints which is very similar and the second answer makes the suggestion that I tried. However when I use it rails ignores the column that I’ve made and just grabs the first one in the table:

>> u = User.find(1)
  User Load (3.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
=> #<User id: 1, email: "XXXXXXX@gmail.com", created_at: "2012-03-15 22:34:39", updated_at: "2012-03-15 22:34:39", primary_account_id: nil>
>> u.primary_account
  Account Load (0.1ms)  SELECT "accounts".* FROM "accounts" WHERE "accounts"."user_id" = 1 LIMIT 1
=> #<Account id: 5, name: "XXXXXX", created_at: "2012-03-16 04:08:33", updated_at: "2012-03-16 17:57:53", user_id: 1>
>> 
  • 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-31T19:33:23+00:00Added an answer on May 31, 2026 at 7:33 pm

    So I created a simple ERD and your issue is very simple, but I think I found a serious issue:

    simple erd

    class User < ActiveRecord::Base
       has_many :accounts
       has_one :primary_account, :class_name => "Account", :primary_key => "account_pimary_id"
    end
    
    class Account < ActiveRecord::Base
       belongs_to :user
    end
    

    To get the associations as is, just set the :primary_key on has_one :primary_account so that it uses users.account_primary_id instead of users.id.

    While this works, it will proboably cause nothing but problems. If Account’s user_id is used as the foreign key for id and account_primary_id, you have no idea if an Account is a normal Account or a Primary Account without explicitly joining both id and account_primary_id every time. A foreign_key should only point at 1 column, in this case, User’s table id. Then it is a straight shot into the Account’s table.

    @Zabba solution is the smart one, but just needs the :include for the join

    has_one :primary_account, :class_name => "Account", :conditions => "users.primary_account_id = accounts.id", :include => :user
    

    This means all Accounts belong to a User and only 1 is flagged as a primary account. Nice and straight forward, avoiding the wacky where clauses.

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

Sidebar

Related Questions

I have a User model that has the following default_scope: default_scope where(account_id: Account.current_account.id) If
I hav two models, User and Account, they have a many-to-many relationship through AccountUsers.
I have a simple databasescheme: User, Account. User has 1-to-many relationship with Account. I
I have an Account model with the following : has_one :primary_user, :class_name => User,
I have a User and Account models with has_one association and nested attributes. My
I have a User model and a Submission model. Each Submission has a ForeignKey
I have User, Account, and Role models. Role stores the relationship type between Account
I have an Account model and a User model: class Account < ActiveRecord::Base has_many
I have User , Account , and Role models. A User can exist by
Suppose I have an Account model which has_many Users. Accounts have a boolean active

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.