this is wrong
as in
> validates_uniqueness_of :email, :if => proc {|email| !email.blank? }
the |email| in proc will return object of record (not the attribute), so you
hav eto use :if => proc {|r| !r.email.blank?}
tom
On Jul 22, 2011, at 17:39 , Fernando Almeida wrote:
> You can use
> validates_uniqueness_of :email, :if => proc {|email| !email.blank? }
>
> Search for conditional validations for more info.
>
>
>
> 2011/7/22 Piter Fcbk <[email protected]>
> I have a model with a non obligatory field that must be unique. The code
> looks like this:
>
> class Person < ActiveRecord::Base
> ...
> validates :email,
> :uniqueness => { :case_sensitive => false },
> :length => { :maximum => 128 },
> :email_format => true,
> :allow_nil => true
> ...
> end
>
> When the form is submitted, if the email text field is empty, and empty
> string is set as email to the Person object. The problem comes when trying to
> create next person with no email since its storing "" instead of nil.
> In order to solve this I want to store nil for the empty string (for this
> field only).
>
> Should I do the check and set on the controller or in the model? I would go
> for the model, but I'm open to suggestions.
> In case its done in the model, should be done by overriding the attribute
> setter or using a call back triggered by .save like before_save?
>
> Thanks for the help
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
>
> --
> Fernando Almeida
> www.fernandoalmeida.net
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
--
===============================================================================
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz
===============================================================================
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.