Hopefully this is an easy question…
I want to validate the uniqueness of a field, unless that field is
blank… ie they didn’t put anything in for it…
Any ideas? Thanks guys.
Hopefully this is an easy question…
I want to validate the uniqueness of a field, unless that field is
blank… ie they didn’t put anything in for it…
Any ideas? Thanks guys.
On 8/28/07, sw0rdfish [email protected] wrote:
Hopefully this is an easy question…
I want to validate the uniqueness of a field, unless that field is
blank… ie they didn’t put anything in for it…
validates_uniqueness_of :field, :if Proc.new {|model|
!model.field.blank?}
what does Proc.new do? I get the rest, little fuzzy on that one.
Awesome thanks!
I figured out that the => was missing… worked perfectly! Thanks
Adam
On 8/28/07, sw0rdfish [email protected] wrote:
what does Proc.new do? I get the rest, little fuzzy on that one.
validates_uniqueness_of :field, :if Proc.new {|model| !model.field.blank?}
there’s actually a typo there, it should be:
validates_uniqueness_of :field, :if => Proc.new {|model|
!model.field.blank?}
Using Proc.new is just a short hand of using the following:
validates_uniqueness_of :field, :if => :some_field_is_not_blank?
def some_field_is_not_blank?
!self.some_field.blank?
end
Here’s some information about Proc from the Pickaxe book:
Proc objects are blocks of code that have been bound to a set of local
variables. Once
bound, the code may be called in different contexts and still access
those variables
def gen_times(factor)
return Proc.new {|n| n*factor }
end
times3 = gen_times(3)
times5 = gen_times(5)
times3.call(12) —> 36
times5.call(5) —> 25
times3.call(times5.call(4)) —> 60
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs