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>
>>
So I created a simple ERD and your issue is very simple, but I think I found a serious issue:
To get the associations as is, just set the
:primary_keyonhas_one :primary_accountso that it usesusers.account_primary_idinstead ofusers.id.While this works, it will proboably cause nothing but problems. If Account’s
user_idis used as the foreign key foridandaccount_primary_id, you have no idea if an Account is a normal Account or a Primary Account without explicitly joining bothidandaccount_primary_idevery time. Aforeign_keyshould only point at 1 column, in this case, User’s tableid. Then it is a straight shot into the Account’s table.@Zabba solution is the smart one, but just needs the
:includefor the joinThis 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.