I am mapping 2 models:
User
Account
class Account
has_many :users
class User
has_one :account
The user table as the account_id in i
It would probably be simpler to add a primary_user_id field to Account, and add a 'has_one' association for the primary_user:
class Account
has_many :users
has_one :primary_user, :class_name => "User"
end
class User
has_one :account
end
If you must use the existing schema (with the :is_primary boolean flag), you can add a scope like this:
class User
has_one :account
scope :primary, where(:is_primary => true)
end
and then chain the scope to the users lookup:
account = Account.find(1)
primary_user = account.users.primary.first