Forum: Ruby on Rails validates_uniqueness_of :username, :if => !self.new_record?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joshua M. (Guest)
on 2006-01-23 21:49
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
Jonathan V. (Guest)
on 2006-01-23 22:02
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
Joshua M. (Guest)
on 2006-01-23 22:37
Thanks a lot. What possible values does :on have?
Francois B. (Guest)
on 2006-01-23 22:39
(Received via mailing list)
Hi !

2006/1/23, Joshua M. <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 !
Francois B. (Guest)
on 2006-01-23 22:42
(Received via mailing list)
2006/1/23, Joshua M. <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

Bye !
Joshua M. (Guest)
on 2006-01-23 22:44
Thanks a lot.
This topic is locked and can not be replied to.