Be sure to validate on the server. If there is a bug in your application (or malicious attacks) and enter inconsistent data in the database, it will be very difficult to fix.
I recommend that you do validation in at least 2 places:
- Data bank, through constraints (not null, Foreign Keys and Unique Indexes)
- Application (Rails model layer)
Putting validation in Javascript is more of a convenience for the user (he doesn’t need to submit the form to see that some field is incorrect), that’s up to you. But what will ensure the same security are the two approaches above.
I always suggest putting safety and reliability above performance. Moreover, I do not believe that the difference in performance is considerable in this case.
See these two related questions:
Tip:
You can use the Gem Foreigner to integrate Foreign Keys with Rails database versioning (Migration).
Remember that there are methods that nay trigger the Rails validations:
- decrement!
- decrement_counter
- increment!
- increment_counter
- toggle!
- touch
- update_all
- update_attribute
- update_column
- update_columns
- update_counters
You can also explicitly skip the validation in this way:
- save(validate: false)
Source: Official Rails Guide to Validations