Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Bug: passing nil as a value raises an undefined method exception

When passing a nil value with the case_sensitive => false option,
when downcase is called, a NoMethodError was raised.  Downcase is
now only called if the value is a string.
  • Loading branch information...
commit b2dffb20dbf473193655c894c38f1a9d96db3865 1 parent 8768787
Gary Rennie Gazler authored
3  lib/uniqueable/validates_uniqueable.rb
@@ -14,7 +14,8 @@ def validate_each(record, attribute, value)
14 14 if options[:case_sensitive]
15 15 result = klass.where(key => value)
16 16 else
17   - result = klass.find(:all, :conditions => ["lower(#{key}) = ?", value.downcase])
  17 + value = value.downcase if value.kind_of?(String)
  18 + result = klass.find(:all, :conditions => ["lower(#{key}) = ?", value])
18 19 end
19 20
20 21 unless result.empty?
5 spec/uniqueable/validates_uniqueable_spec.rb
@@ -72,6 +72,11 @@ class AdminUser < ActiveRecord::Base
72 72 user2.should_not be_valid
73 73 end
74 74
  75 + it "should not raise an exception when a value is nil" do
  76 + lambda { User.create(:name => nil, :username => nil) }.should_not raise_error
  77 +
  78 + end
  79 +
75 80 it "should not affect an organisation with a unique alias" do
76 81 organisation = Organisation.create(:name => "Organisation", :alias => "org")
77 82 organisation.should be_valid

0 comments on commit b2dffb2

Please sign in to comment.
Something went wrong with that request. Please try again.