Is it necessary to create validations in the model for a field that the user does not affect?
By necessary I mean is a best-practice.
I have a before_create filter that sets a user token to a user account. Should I add validations even though I'm (the backend code) the only thing that has power over this field?
Thanks.
I do. It allows me to keep data integrity in my database.
You need to look at the order of Active record callbacks,
The most-up-to-date version of this list for the latest version of Rails can be found in the http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html . The lists for Rails 3 & 2 are below.
The most up-to-date version of this list can be found in the Rails 3 guides .
before_validation
after_validation
before_save
around_save
before_create
around_create
after_create
after_save
In this case as you are setting the value after the validation, the validation error will trigger everytime
Source - What is the order of ActiveRecord callbacks and validations?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.