[Rails]
Re: validates_uniqueness_of :username, :if => !self.new_reco
Jonathan Viney
jviney at spreydon.org.nz
Mon Jan 23 20:02:57 GMT 2006
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 Muheim 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
--
Posted via http://www.ruby-forum.com/.
More information about the Rails
mailing list