0

In rails i have two models, Groups and Users. How can i do that the user have only one group and a goupr have max 4 users?
I have tried this in users.rb belongs_to :group and this in groups.rb has_many :users

Thanks

vretamal
  • 1,485
  • 1
  • 10
  • 14
  • Have you checked the [Rails Guides](http://guides.rubyonrails.org/association_basics.html#the-has-many-association)? Then you may want to have a validation like in this [Thread](http://stackoverflow.com/questions/4836897/validate-the-number-of-has-many-items-in-ruby-on-rails)?! – BBQ Chef Sep 07 '13 at 15:07

1 Answers1

0

Use a custom validation for example:

class Group
  has_many :users
  validate :limit_users

  private

  def limit_users
    errors.add('Only 4 users allowed') if users.size > 4
  end
end

class User
  belongs_to :group
end
thelucid
  • 36
  • 5