How to create association with limit of items on rails

63 Views Asked by At

I have a model with doctor and patients with associations:

Class doctor has_may :patients Class patient belongs_to :doctor

I need to make limit of free payment patients and other limit for payed patients. Who can i it?

For now my doctor has no limit patients. It's not right.

1

There are 1 best solutions below

3
anonymus_rex On

You can do it using length validation like this:

class Doctor
  has_many :patients
  validates_length_of :patients, maximum: 10
end

class Patient
  belongs_to :doctor
  validates_associated :doctor
end

Check this answer from @jstejada.