save! not allowed after validates_uniqueness

Can you please explain why you think the validation should be


Hi Nicholas,

Thanks for your response. What I meant was that validation should not
compare the object that is being updated against its own existing copy.
For example, I am editting an existing object:

   [:aspect => "Aspect-A", :user_id => 5, :third_field => "morning"].

Now, the object is unique in the database according to

   validates_uniqueness_of :aspect, :scope => :user_id

That's all good. However, when I update the object with the following

   self.third_field = "night"

Rails returns an error:

   ActiveRecord::RecordInvalid: Validation failed: Aspect has
already been taken

Why? I am just trying to update an existing object, not create a new
one that would violate the uniqueness validation.


What code are you using for creating and modifying the object before saving it?


Hi Colin,

Thanks for asking that question. I had preloaded the database so I
didn't have to create a new record. So now I just tried doing that and
it wouldn't let me create a new record although the :aspect value I used
does not exist at all in the database. Console output is below:

stat0 = => user0, :aspect => "aspect0")

=> #<Stat id: nil, user_id: 1000, aspect: "aspect0", . . .>!

ActiveRecord::RecordInvalid: Validation failed: Aspect has already been
  from (irb):70

"aspect0" is something I completely made up and does not exist in the
database. :user_id 1000 does exist in different rows with different
values of :aspect however.