Reputation: 330
I have a table Student(class_id(integer),marks(integer),rank(integer))
In my Seeds.rb file i have written something like this to make entry to table
Student.create(class_id:2 ,marks:"abcdef", rank: 2)
when i add these data to table by rake db:seed command ,i was expecting i would not be allowed to add this ,because for marks field of student, string is being inserted instead of integer.But rails added this record without any problem.
so how do i ensure this kind of entry doesnot happen and my table ensures type check.Me a newbie to rails and postgres
Upvotes: 0
Views: 119
Reputation: 118261
So, a validation like below will work. Add it to the Student
model.
validates :marks, numericality: { only_integer: true }
Whenever you will try to create a Student
record, this validation will be called by Rails. If it passes then the recored will be created, otherwise not. There are methods to check if a record is valid or not in rails.
Read the guide to know how validation works in rails.
Upvotes: 1