# Validates\_uniqueness\_of

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of/112147>\
**Category:** Rails\
**Created:** [August 15, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/112147 "2007-08-15T22:21:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [August 15, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/112147/1 "2007-08-15T22:21:39Z")

</div>

Hi,

In my user model I validates\_uniqueness\_of :login.

In my app, users can edit their information.  
If the user change the name but not the login, the record gets  
rejected.  
Of course the login is already used, but by the same user that it is  
editing his information.

The record should not be rejected right ?  
Could someone tell me if it is normal that it gets rejected ?

Thanks,  
Mickael.

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [August 16, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/112147/2 "2007-08-16T22:48:16Z")

</div>

So, nobody thinks that validates\_uniqueness\_of should accept the  
edition of the same record ?  
Should I add an on create condition and validate the record “by hand”  
on edition ?  
Mickael.

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [August 16, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/112147/3 "2007-08-16T23:48:24Z")

</div>

you might need to :scope the contraint:  
[http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000944](http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000944)

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [August 17, 2007, 12:30am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/112147/4 "2007-08-17T00:30:14Z")

</div>

Mickael Faivre-macon wrote:

> So, nobody thinks that validates\_uniqueness\_of should accept the  
> edition of the same record ?  
> Should I add an on create condition and validate the record “by hand”  
> on edition ?  
> Mickael.

You could try using something like validates\_uniqueness\_of :login,  
:except =\> ‘update’

I think that’s the way it goes, that way it won’t validate on the update  
action only on the login or what ever. Hope that helps.

~Jeremy

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [August 17, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/112147/5 "2007-08-17T21:45:35Z")

</div>

Jeremy:  
Okay, if it’s the only solution, I will do the update validation by  
hand. Thanks Jeremy.  
I was hoping Rails was a little bit more “intelligent” and it would  
detect that we are updating the same record, so it is OK to set the  
same value as before.

Jemminger:  
I don’t think a scope can help me there. I want to validates the  
uniqueness of this field on the global scope.  
Thanks anyway.

Mickael.

On Aug 17, 12:30 am, Jeremy W. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [August 18, 2007, 1:29am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/112147/6 "2007-08-18T01:29:58Z")

</div>

I agree that it should be more intelligent. I would think that :except  
=\> ‘update’ would lead to inconsistencies unless you added a before  
update filter that checked this on your own…I think a select count  
where id != self.id and attrib = attrib would do it. I may be stating  
the obvious here, but I was quite surprised myself to learn that it  
behaves this way.

MickTaiwan wrote:

> Thanks anyway.
> 
> > > edition of the same record ?  
> > > ~Jeremy  
> > > –  
> > > Posted viahttp://www.ruby-forum.com/.
> 
> >

–  
Sincerely,

William P.

[http://www.billpratt.net](http://www.billpratt.net)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [August 18, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/112147/7 "2007-08-18T21:46:57Z")

</div>

I played around with this a little, and I was mistaking yesterday. I am  
seeing the correct behavior on updates with validates\_uniqueness\_of. If  
the duplicate is in the record I am updating, the validation passes.

in my asset.rb model:  
…  
validates\_uniqueness\_of :asset\_tag, :scope =\> :company\_id  
…

in irb:

> > a = Asset.find(:first)  
> > …  
> > a.asset\_tag  
> > =\> “AS00003”  
> > a.new\_record?  
> > =\> nil  
> > a.asset\_tag = “AS00003”  
> > =\> “AS00003”  
> > a.new\_record?  
> > =\> nil  
> > a.valid?  
> > =\> true

William P. wrote:

> > :except =\> ‘update’ would lead to inconsistencies unless you added a
> > 
> > > I was hoping Rails was a little bit more “intelligent” and it would
> > > 
> > > > >
> > 
> > removed\_email\_address@domain.invalid
> 
> –  
> Sincerely,
> 
> William P.
> 
> [http://www.billpratt.net](http://www.billpratt.net)  
> removed\_email\_address@domain.invalid
> 
> >

–  
Sincerely,

William P.

[http://www.billpratt.net](http://www.billpratt.net)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [August 18, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/112147/8 "2007-08-18T21:16:52Z")

</div>

According to the source, this problem should not happen:

547: unless record.new\_record?  
548: condition\_sql \<\< " AND  
#{record.class.table\_name}.#{record.class.primary\_key} \<\> ?"  
549: condition\_params \<\< record.send(:id)  
550: end

I’ll dig in to it next week and see whats happening.

William P. wrote:

> > Okay, if it’s the only solution, I will do the update validation by  
> > Mickael.
> > 
> > > > on edition ?  
> > > > Posted viahttp://www.ruby-forum.com/.
> 
> [http://www.billpratt.net](http://www.billpratt.net)  
> removed\_email\_address@domain.invalid

–  
Sincerely,

William P.

[http://www.billpratt.net](http://www.billpratt.net)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [August 18, 2007, 11:20pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/112147/9 "2007-08-18T23:20:08Z")

</div>

Thanks for the update.

I had implemented a solution “by hand”, with an :on=\>:create and doing  
the validation by hand on update.

After seeing the source you just sent, I reverted to the old (and to  
me “normal”) way of doing it, and then it worked. I don’t know why it  
failed earlier.

Sorry for the fuss, and again thank you for the work you’ve done.

Mickael.

On 8/18/07, William P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> in irb:
> 
> > > a.valid?  
> > > 549: condition\_params \<\< record.send(:id)
> 
> uniqueness of this field on the global scope.
> 
> ## I think that’s the way it goes, that way it won’t validate on the update
> 
> William P.
> 
> [http://www.billpratt.net](http://www.billpratt.net)  
> removed\_email\_address@domain.invalid
> 
> >

–  
Mickael.  
Coding an AI ! [http://faivrem.googlepages.com/antbattle](http://faivrem.googlepages.com/antbattle)
