Active Record Validations - Ruby on Rails Guides - 0 views
-
validates :name, presence: true
-
Model-level validations are the best way to ensure that only valid data is saved into your database.
- ...117 more annotations...
-
Database constraints and/or stored procedures make the validation mechanisms database-dependent and can make testing and maintenance more difficult
-
combined with other techniques, client-side validation can be a convenient way to provide users with immediate feedback
-
Active Record uses the new_record? instance method to determine whether an object is already in the database or not.
-
Creating and saving a new record will send an SQL INSERT operation to the database. Updating an existing record will send an SQL UPDATE operation instead. Validations are typically run before these commands are sent to the database
-
After Active Record has performed validations, any errors found can be accessed through the errors.messages instance method
-
To verify whether or not a particular attribute of an object is valid, you can use errors[:attribute]. I
-
All of them accept the :on and :message options, which define when the validation should be run and what message should be added to the errors collection if it fails, respectively.
-
'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).
-
use this helper when your model has associations with other models and they also need to be validated
-
-
This validation creates a virtual attribute whose name is the name of the field that has to be confirmed with "_confirmation" appended.
-
The exclusion helper has an option :in that receives the set of values that will not be accepted for the validated attributes.
-
validates the attributes' values by testing whether they match a given regular expression, which is specified using the :with option.
-
:wrong_length, :too_long, and :too_short options and %{count} as a placeholder for the number corresponding to the length constraint being used.
-
-
validate associated records whose presence is required, you must specify the :inverse_of option for the association
-
an association is present, you'll need to test whether the associated object itself is present, and not the foreign key used to map the association
-
validate the absence of a boolean field you should use validates :field_name, exclusion: { in: [true, false] }.
-
a :scope option that you can use to specify other attributes that are used to limit the uniqueness check
-
a :case_sensitive option that you can use to define whether the uniqueness constraint will be case sensitive or not.
-
To implement the validate method, you must have a record parameter defined, which is the record to be validated.
-
the validator will be initialized only once for the whole application life cycle, and not on each validation run, so be careful about using instance variables inside it.
-
The block receives the record, the attribute's name and the attribute's value. You can do anything you like to check for valid data within the block
-
the :message option lets you specify the message that will be added to the errors collection when validation fails
-
You can do that by using the :if and :unless options, which can take a symbol, a string, a Proc or an Array.
-
:expiration_date_cannot_be_in_the_past, :discount_cannot_be_greater_than_total_value
-
errors[:base] is an array, you can simply add a string to it and it will be used as an error message.
-
use this method when you want to say that the object is invalid, no matter the values of its attributes.
-
calling errors.clear upon an invalid object won't actually make it valid: the errors collection will now be empty, but the next time you call valid? or any method that tries to save this object to the database, the validations will run again.