-1

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 Answers1

1

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.

anonymus_rex
  • 470
  • 4
  • 18
  • Ok, and how to split payed patients and free patients – Andrew Kancev Nov 23 '22 at 19:10
  • You have 2 ways of doing this. The first one, and the one which I recommend, is to set scopes on `Patient` model to query each group (paids and frees): `scope :paids, -> { where(paid: true) }` and `scope :frees, -> { where(paid: false) }`, then you can get paid patients doing something like `Doctor.find(1).patients.paids`. The other way would be to split `has_many` statement on two: `has_many :paid_patients` and `has_many :free_users`. To add one paid patient you should do something like `Doctor.find(1).paid_patients << Patiend.create(paid: true)` – anonymus_rex Nov 24 '22 at 17:46
  • Thank you very much. I have a enum with patients [free, payed]. I think i make it by second way – Andrew Kancev Nov 25 '22 at 18:38