0

If I have a has_and_belongs_to_many relationship between two models, let's say Users and Accounts, can I require that a User have at least one Account, and how?

Also, using the has_and_belongs_to_many relationship, is it possible for an Account not to have a User?

What I need is a relationship where Accounts can live on their own, and belong to Billers, but they can also belong to Users if a User signed up with one. Is this possible, and how?

Azolo
  • 4,353
  • 1
  • 23
  • 31

2 Answers2

0

I personally would drop the the HABTM. Instead I would use has_many :though=>

You would need to create two new models, account_users, and account_billers. You likely already have join tables for the HABTM, but this will expose them as models so they will need ID fields.

So you would end up with something like the following:

class Account < ActiveRecord::Base
  has_many :account_billers
  has_many :account_users

  has_many :billers, :through=> :account_billers
  has_many :users, :through=> :account_users
end

class User < ActiveRecord::Base
  has_many :account_users
  has_many :accounts, :through=>:account_users

  validates :accounts, :length => { :minimum => 1}
end

class Biller < ActiveRecord::Base
  has_many :account_billers
  has_many :accounts, :through=>:account_billers

  validates :accounts, :length => { :minimum => 1}
end

class AccountUser < ActiveRecord::Base
  belongs_to :user
  belongs_to :account
end

class AccountBiller < ActiveRecord::Base
  belongs_to :biller
  belongs_to :account
end
Ben Miller
  • 1,464
  • 12
  • 13
  • This would setup a requirement that users and billers have accounts, but accounts could exist with out having a user or a biller. – Ben Miller Jul 12 '12 at 19:36
0

To validate presence of at least one association, you might want to use a custom validation method, something like

class User < ActiveRecord::Base
  has_and_belongs_to_many :accounts
  validate :require_at_least_one_account

  private
    def require_at_least_one_account
      errors.add(:accounts, "must amount to at least one") if accounts.size < 1
    end
end

(Although this brings a question of how an account is shared between users)

For your second question, looks like polymorphic associations are what you're looking for, but you can't do this straight with a HABTM relationship, you'll have to change it to a has_many :through and introduce a join model.

HargrimmTheBleak
  • 2,147
  • 1
  • 19
  • 19
  • You could use polymorphic, but there is no good way to do FK constraints with polymorphism. If user1521444 doesn't have a great grasp of the models and relations yet, I would avoid this. – Ben Miller Jul 13 '12 at 01:43
  • With polymorphic associations it seems as if you can then have an account belong to either a User or Biller, but can the same account belong to both under a polymorphic association? – macdamaniac Jul 13 '12 at 16:13
  • If you can have accounts belonging to both users and billers at the same time, I don't think AR supports this out of the box. You can check this question http://stackoverflow.com/questions/3209322/rails-polymorphic-has-many – HargrimmTheBleak Jul 13 '12 at 16:23