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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You can use validates_uniqueness_of :email, :if => proc {|email| !email.blank? } Search for conditional validations for more info. 2011/7/22 Piter Fcbk <piter.fcbk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 <piter.fcbk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Excuse me, i dont remembered the exact syntax. I looked my code now, i use that way :if => proc {|r| !r[''email''].blank?} Thanks Tom. 2011/7/22 Tom Meinlschmidt <tomas-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org>> > 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 <piter.fcbk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
First than all I want to thanks you for the help, really appreciate it. I have a question regarding the solution proposed. I also have a unique key in the database in order to avoid race conditions. That is the reason for which I was thinking on setting nil when an blank String is set, probably should said this before (apologize for that). Would this solution, validates_uniqueness_of :email, :if => proc {|email| !email.blank? }, work or it will just avoid the application level validation but still crash with and error trying to insert a duplicate value on the database? In case it doesn''t work, what would you recommend? Thanks again. On Fri, Jul 22, 2011 at 12:54 PM, Fernando Almeida < fernando-7WGqr3rU1tV1NwFxuVVnt9HuzzzSOjJt@public.gmane.org> wrote:> Excuse me, i dont remembered the exact syntax. > > I looked my code now, i use that way > > > :if => proc {|r| !r[''email''].blank?} > > Thanks Tom. > > > > 2011/7/22 Tom Meinlschmidt <tomas-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org> > >> >> 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 <piter.fcbk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 22, 2011, at 1:38 PM, Piter Fcbk wrote:> First than all I want to thanks you for the help, really appreciate > it. > > I have a question regarding the solution proposed. I also have a > unique key in the database in order to avoid race conditions. > That is the reason for which I was thinking on setting nil when an > blank String is set, probably should said this before (apologize for > that). > > Would this solution, validates_uniqueness_of :email, :if => proc {| > email| !email.blank? }, work or it will just avoid the application > level validation but still crash with and error trying to insert a > duplicate value on the database? > > In case it doesn''t work, what would you recommend?You could have a: before_validation :nilify_blank_email def nilify_blank_email self.email = nil if self.email.blank? end -Rob> > Thanks again. > > On Fri, Jul 22, 2011 at 12:54 PM, Fernando Almeida <fernando-7WGqr3rU1tV1NwFxuVVnt9HuzzzSOjJt@public.gmane.org > > wrote: > Excuse me, i dont remembered the exact syntax. > > I looked my code now, i use that way > > > :if => proc {|r| !r[''email''].blank?} > > Thanks Tom. > > > > 2011/7/22 Tom Meinlschmidt <tomas-ooGa/4BNRfTT2+6r9I86XQ@public.gmane.org> > > 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 <piter.fcbk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > > 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 rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > 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 rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > . > > > -- > 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 rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > .Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.com/ -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I''m looking into adding feature toggles to my app Background reading: http://blog.jayfields.com/2010/10/experience-report-feature-toggle-over.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+jayfields%2FmjKQ+%28Jay+Fields+Thoughts%29 http://martinfowler.com/bliki/FeatureToggle.html And Jez and David''s book on Continuous Deployment Basic principle is that you have feature toggles instead of feature branches for long running features (stuff you can''t deploy to production same day you start). In addition to being able to hide features while avoiding the potentially ugly merges you can get with multiple long running feature branches it also allows you to do cool stuff like automatically rolling out features to only a subset of users, a/b split testing of features, etc. Wondering (a) what best practices are from an implementation perspective (if any) in Rails and (b) if there are any nice gems to wrap the whole process of being able to declaratively describe rules about what features what classes of users would see in what environments, etc. Any thoughts/ideas/suggestions appreciated. Best Wishes, Peter -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.