Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validates presense vs null false in Rails models/tables

I was playing around with Rails admin and I noticed something.

Attributes which are defined as below in model, counted as "Required" in Rails admin

 validates :user, presence: true

However, attributes which are defined as below in table (schema/migration) still counted as "Optional"

t.datetime "created_at",:null => false

I would have assumed that both of these are identical except perhaps the level from which the validation error pops up. Am I wrong or is this a Rails admin error? Are both of these ensuring that this field will be required for a successful field save or is there a difference?

like image 443
Karthik T Avatar asked Nov 14 '13 08:11

Karthik T


People also ask

What is the difference between validate and validates in rails?

validates is used for normal validations presence , length , and the like. validate is used for custom validation methods validate_name_starts_with_a , or whatever crazy method you come up with. These methods are clearly useful and help keep data clean. That test fails.

What does null false mean rails?

:null => false in a Rails migration tells your database not to accept NULL values. It can be used with :default => 0 to tell your database to use '0' as the default value (a) when NULL or nothing is specified in a query or (b) when creating or updating an object.

How does validate work in Rails?

Rails validation defines valid states for each of your Active Record model classes. They are used to ensure that only valid details are entered into your database. Rails make it easy to add validations to your model classes and allows you to create your own validation methods as well.

What is null false in rails migration?

The null: false parameter means this column does not allow NULL values. The default: false tells us this column will default to false whenever a value isn't specified. This is nice because we can guarantee this field will always be either true or false now. It can't be null because the database prevents it.


2 Answers

Adding a :null => false means that this is a database restriction, i.e. under no circumstance will the database allow a null value.

Adding a presence: true is a model level validation so will take place before the object is inserted into the database. There may be a case you want to break these validations (for example edge cases or in your specs) You can then skip validation using :validates => false and the object will still go into the database, with a null DB restriction, this won't happen.

like image 154
Yule Avatar answered Sep 20 '22 03:09

Yule


t.datetime "created_at",:null => false 

is telling the database not to accept null values. Whereas, validates :user, presence: true is like telling the Rails app to not accept null values. But it would be good to have them integrated, like having :null => false to also have it registered with Rails to have a model validation.

like image 44
aartikriplani Avatar answered Sep 23 '22 03:09

aartikriplani