Is validates_uniqueness_of transaction safe?

Hi All,

I’ve recently starting programming in RoR. Here’s a question re: the
ActiveRecord validation methods. Are they transaction safe?

It seems that saving an instance of a model with validation methods
could require multiple db queries. For example,
validated_uniqueness_of would need to do at least one query to figure
out if the value already exists and then the second query to insert a
new record.

Are these queries contained in a single transaction? If not, how do
you make sure that they are part of the same transaction?

Thanks for your help!

dparkmit wrote:

you make sure that they are part of the same transaction?
Yes validates_uniqueness_of may fail when there concurrent
validations because it doesn’t lock the table. To be sure
you’d have to either lock the table in a before_validation
callback or add a unique index to the table and handle the
error.


We develop, watch us RoR, in numbers too big to ignore.