-1

My table name: Records

Fields:

  1. School_id,
  2. Admission_id

Association:

class Record < ActiveRecord::Base belongs_to :school

For example So i need to have a made an uniqueness such that admission id should be unique on school alone, different school to have same admission id, but same school cant have same admission id again while creating the record

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
jegan
  • 31
  • 7

1 Answers1

1

You can add a constraint to check for the uniqueness with a scope like this

class Record < ActiveRecord::Base
  belongs_to :school
  validates :admission_id, uniqueness: { scope: :school_id }
end

Here is the link to documentation of uniqueness.

Abdul Rehman
  • 670
  • 7
  • 12