I have a table named posts, and i want some fields to be not null. so whenever there is no data, i want to produce a msg. How can i make the fields as not null?
I have a table named posts , and i want some fields to be
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you create the table, just add “not null” after the data type for the column, e.g. “text not null”. This will cause an exception to be thrown if you try to add a record with a null value for that column. You can add a catch/rescue in your rails application to catch this exception.
However, a better way would probably be to validate the data before you even try to insert into the database using an ActiveRecord callback…