Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails model that has both 'has_one' and 'has_many' but with some constraints

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 it.

Now on the Account model I want to create a 'primary user' which an account only has 1 off. The user table has a boolean flag :is_primary, how can I create a has_one on the account side for a user who has the is_primary and account_id mapped.

So the SQL would look like:

SELECT * FROM users where account_id=123 and is_primary = 1

So I want:

A user has an account. An account has many users, and has a single primary user also.

like image 355
Blankman Avatar asked Feb 20 '12 16:02

Blankman


People also ask

What is the difference between Has_one and Belongs_to?

They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.

What is a polymorphic association in Rails?

Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.

What is Has_and_belongs_to_many?

Rails offers two different ways to declare a many-to-many relationship between models. The first way is to use has_and_belongs_to_many, which allows you to make the association directly: The second way to declare a many-to-many relationship is to use has_many :through.


2 Answers

Approach 1 - Add a new association

Add an has_one association with a where lambda. This allows you to work within your current schema.

class Account    has_many :users   has_one  :primary_user, -> { where(is_primary: true) }, :class_name=> "User" end 

Now:

account.users #returns all users associated with the account account.primary_user #returns the primary user associated with the account # creates a user with is_primary set to true account.build_primary_user(name: 'foo bar', email: '[email protected]') 

Approach 2 - Add an association method

class Account    has_many :users do     def primary       where(:is_primary => true).first     end   end end 

Now:

account.users.primary # returns the primary account 
like image 99
Harish Shetty Avatar answered Nov 03 '22 00:11

Harish Shetty


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
like image 36
PinnyM Avatar answered Nov 02 '22 23:11

PinnyM