# Validates\_uniqueness\_of :username, :if =\> !self.new\_record?

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-username-if-self-new-record/53043>\
**Category:** Rails\
**Created:** [January 23, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-username-if-self-new-record/53043 "2006-01-23T20:49:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [January 23, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-username-if-self-new-record/53043/1 "2006-01-23T20:49:28Z")

</div>

Hi all

I want users to register on my page. To register, they only have to  
deliver an email and a password. Then a link with an activation token  
will be sent to them, and after clicking the link the user sees a page  
where he should fill in further details, means: a username.

My model looks like this:

class Member \< ActiveRecord::Base  
validates\_presence\_of :username, :if =\> !self.new\_record?  
end

Sadly this does not work: it tells me “undefined method `new\_record?’  
for Member:Class”.

Where’s the problem? Shouldn’t any ActiveRecord class have a method  
called ‘new\_record?’?

Thanks for help.  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [January 23, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-username-if-self-new-record/53043/2 "2006-01-23T21:02:57Z")

</div>

I think you should probably use the :on option here:

validates\_presence\_of :username, :on =\> :update

This will only perform the validation if the record has already been  
saved. Or, you can do this (not recommended):

validates\_presence\_of :username, :if =\> Proc.new { |member|  
!member.new\_record? }

The reason you can’t do it the way you tried is because self in that  
context is referring to the Member class, not to any particular  
instance.

Cheers, Jonny.

Joshua M. wrote:

> Hi all
> 
> I want users to register on my page. To register, they only have to  
> deliver an email and a password. Then a link with an activation token  
> will be sent to them, and after clicking the link the user sees a page  
> where he should fill in further details, means: a username.
> 
> My model looks like this:
> 
> class Member \< ActiveRecord::Base  
> validates\_presence\_of :username, :if =\> !self.new\_record?  
> end
> 
> Sadly this does not work: it tells me “undefined method `new\_record?’  
> for Member:Class”.
> 
> Where’s the problem? Shouldn’t any ActiveRecord class have a method  
> called ‘new\_record?’?
> 
> Thanks for help.  
> Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [January 23, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-username-if-self-new-record/53043/3 "2006-01-23T21:37:58Z")

</div>

Thanks a lot. What possible values does :on have?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [January 23, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-username-if-self-new-record/53043/4 "2006-01-23T21:42:16Z")

</div>

2006/1/23, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks a lot. What possible values does :on have?

:save (Always)  
:create (new\_record? == true)  
:update (new\_record? == false)

:destroy ?  
:delete ?

Not sure about those two. Check the documentation at  
[http://api.rubyonrails.com](http://api.rubyonrails.com)

Bye !

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [January 23, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-username-if-self-new-record/53043/5 "2006-01-23T21:39:07Z")

</div>

Hi !

2006/1/23, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> class Member \< ActiveRecord::Base  
> validates\_presence\_of :username, :if =\> !self.new\_record?  
> end

If you look at the documentation, you’ll see you need to pass a Proc  
instance:

validates\_presence\_of :username, :if =\> Proc.new { |record|  
!record.new\_record? }

Alternatively, I believe you should be able to do this too:

validates\_presence\_of :username, :on =\> :update

That last part I haven’t tested.

Hope that helps !

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [January 23, 2006, 9:44pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-username-if-self-new-record/53043/6 "2006-01-23T21:44:34Z")

</div>

Thanks a lot.
