@Robert - Yes, I read that portion of the documentation and thanks for
the reminder.
Otherwise, of potential interest to discussants...
I now have a migration adding a unique index to a model, like so:
add_index :projects, [:name, :street_address, :city, :state, :zip],
:unique => true
where only some subset of the attributes -- [name, zip], [name, city,
state], [street_address, city, state], etc. -- are required in the
Project model.
In testing, only when all the properties named in my add_index method
are passed is the exception properly thrown.
For example:
assert_raise ActiveRecord::StatementInvalid do
exceptionable_proj = Project.new
exceptionable_proj.city = city
exceptionable_proj.street_address = street_address
exceptionable_proj.state = state
exceptionable_proj.name = name
exceptionable_proj.zip = zip
exceptionable_proj.save
exceptionable_proj2 = Project.new
exceptionable_proj2.city = city
exceptionable_proj2.street_address = street_address
exceptionable_proj2.state = state
exceptionable_proj2.name = name
exceptionable_proj2.zip = zip
exceptionable_proj2.save
end
The above passes, whereas the test below does not:
assert_raise ActiveRecord::StatementInvalid do
exceptionable_proj3 = Project.new
exceptionable_proj3.street_address = street_address2
exceptionable_proj3.zip = zip2
exceptionable_proj3.save
exceptionable_proj4 = Project.new
exceptionable_proj4.street_address = street_address2
exceptionable_proj4.zip = zip2
exceptionable_proj4.save
end
Grar
On Feb 23, 4:01 pm, Robert Walker <[email protected]> wrote:
> Just to be crystal clear, what a number of these replies are attempting
> to tell you is that you cannot rely on validates_uniqueness_of.
>
> Excerpt from the Rails docs on validation:
> -----------------------------
> Concurrency and integrity
>
> Using this validation method in conjunction with ActiveRecord::Base#save
> does not guarantee the absence of duplicate record insertions, because
> uniqueness checks on the application level are inherently prone to race
> conditions. For example, suppose that two users try to post a Comment at
> the same time, and a Comment’s title must be unique. At the
> database-level, the actions performed by these users could be
> interleaved in the following manner:
> -----------------------------
>
> If two separate requests are received at virtually the same instant then
> validate_uniqueness_of can fail silently. You will end up with
> duplicates in the database that your validation specifically tries to
> avoid.
>
> This is why the unique index across the two columns is necessary. This
> can only be reliably prevented at the database level. If this index
> exists and the validates_uniqueness_of fails due to a race condition the
> database layer will raise and exception. So you need to be prepared for
> this, even when using validates_uniqueness_of, by rescuing from the
> possible exception.
> --
> Posted viahttp://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.