mysql - How to validate uniqueness of field based on another table -


i want validate roll_no field in students model based on student_section model field section,

student_section.rb

class studentsection < activerecord::base   validates :standard_id, :presence=> {:message=>" cannot blank"}    validates :section_id, :presence=> {:message=>" cannot blank"}    validates :student_id, :presence=> {:message=>" cannot blank"} end 

i add validation in student.rb as

class student < activerecord::base   validates :student_id, :presence=> true   validates :student_name, :presence=> true    validates_format_of :email, :with => /\a([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i    validates :phone, :length=>{:in => 8..15}    validates :admission_no, :uniqueness=> { scope: :org_id}     validates :roll_no, :uniqueness=> { scope: @student_section.section_id} end 

but throws unknown field section_id

first, should make relationship between student , studentsection. if understand correctly, may create one-to-many relation. after validation should work fine. however, if search more complex validation logic - may use validates_with statement (validates_with @ apidock.com)


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -