I have a validate_uniqueness_of :field inside my ActiveRecord model. When i do a single create/update it works nicely but i have to do some large batch creation from csv files inside a Transaction
When i am in the transaction the validate_uniqueness_of does not detect the error and the model is saved!
I have a validate_uniqueness_of :field inside my ActiveRecord model. When i do a single
Share
Could it be that the non-unique values are created during the transaction?
The validate methods check before the transaction and then all values are still not present in the table and thus unique.
Edit: Create a index with the unique property turned on for your field and the transaction will fail and thus preventing the addition of non-unique elements.
To do some you should add something this in your migration file
Edit 2: A transaction like this will will give something like a “ActiveRecord::StatementInvalid: Mysql::Error: Duplicate entry ‘123’ for key 1: <sql statement here>” error.