Forum: Ruby on Rails making validates_uniqueness_of case insensitive

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3d155da7f1c04077027c7283c9e99a84?d=identicon&s=25 cole.jeff (Guest)
on 2005-11-13 03:40
(Received via mailing list)
Hi, what is the easiest way to modify my model so that
"validates_uniqueness_of" is case insensitive?  For example, I want a
user's email address to be unique regardless of case.

thanks,
Jeff
5565e460a42fbe5669b458b2753f8d34?d=identicon&s=25 codeslush (Guest)
on 2005-11-13 03:46
(Received via mailing list)
Specifically, for email addresses, the easiest way (though maybe not the
way you want to do it) is to upper or lower case it going into the
database!  ;-) You probably are looking for a more elegant solution,
huh?   This would only be good for email addresses - so perhaps someone
can give a better answer for other fields that would follow a similar
requirement.

Michael
58c44a4a506d878f9a112f1d7b7cb87e?d=identicon&s=25 jeremyevans0 (Guest)
on 2005-11-13 08:53
(Received via mailing list)
On 11/12/05, Jeff Cole <cole.jeff@gmail.com> wrote:
> Hi, what is the easiest way to modify my model so that
> "validates_uniqueness_of" is case insensitive?  For example, I want a
> user's email address to be unique regardless of case.

It's not the Rails way, but a unique index in the database will work.
For example, in PostgreSQL:

CREATE UNIQUE INDEX index_name ON table_name (lower(column_name));
A2b2f4ee23989dc68529baef9cbddcd6?d=identicon&s=25 listbox (Guest)
on 2005-11-13 13:41
(Received via mailing list)
On 13-nov-2005, at 3:40, Jeff Cole wrote:

> Hi, what is the easiest way to modify my model so that
> "validates_uniqueness_of" is case insensitive?  For example, I want
> a user's email address to be unique regardless of case.
>
> thanks,
> Jeff

class Foo <AR::Base
  before_validation :lowercase_email
  private
    def lowercase_email
       email.downcase!
    end
  end
end
3d155da7f1c04077027c7283c9e99a84?d=identicon&s=25 cole.jeff (Guest)
on 2005-11-13 14:53
(Received via mailing list)
I don't want to store the data as lower case.  For example, login
names should also be unique regardless of case, but I want to
preserve the login name with its original casing.

So, it looks like I will have to create my own validation for now.
The question is whether I can mix my own validations with the other
existing standard validations (e.g.
"validates_presence_of :first_name")?

-Jeff
A2b2f4ee23989dc68529baef9cbddcd6?d=identicon&s=25 listbox (Guest)
on 2005-11-13 22:08
(Received via mailing list)
On 13-nov-2005, at 14:52, Jeff Cole wrote:

> I don't want to store the data as lower case.  For example, login
> names should also be unique regardless of case, but I want to
> preserve the login name with its original casing.
>
> So, it looks like I will have to create my own validation for
> now.   The question is whether I can mix my own validations with
> the other existing standard validations (e.g.
> "validates_presence_of :first_name")?

Well, I don't know what reason one would have to store mixed-case
logins (especially if you require logins to be
unique regardless of case) - but you also can try and
add :case_insensitive=>true to validations, make a unit test
and it will be part of Rails
793a1abe9d6824492dcf9aeff6a337ab?d=identicon&s=25 james.rw.walker (Guest)
on 2005-11-13 22:59
(Received via mailing list)
I've seen a need for it with checking for uniqueness in the names of a
set of things, eg 'book' vs 'Book', so I think it's a valid request.

It seems to me that the default behaviour is left to the database
configuration.
66a129f44912c5c466733283ef8ba8d1?d=identicon&s=25 Sushruth Blah (yehweh)
on 2005-11-14 09:50
james.rw.walker wrote:
> I've seen a need for it with checking for uniqueness in the names of a
> set of things, eg 'book' vs 'Book', so I think it's a valid request.
>
> It seems to me that the default behaviour is left to the database
> configuration.

Hi,

I dont know which version you use but validates_uniqueness_of if is case
insensitive. I just did a check, and it works fine for me.

S
793a1abe9d6824492dcf9aeff6a337ab?d=identicon&s=25 james.rw.walker (Guest)
on 2005-11-14 12:22
(Received via mailing list)
--------------- app/model/asset_type.rb:
class AssetType < ActiveRecord::Base
  attr_accessible :name
  has_many :asset
  validates_length_of :name, :within => 1..100, :too_long => "pick a
shorter name", :too_short => "pick a longer name"
  validates_uniqueness_of :name, :message => "an asset type with that
name already exists"
end

--------------- test/unit/asset_type_test.rb:
require File.dirname(__FILE__) + '/../test_helper'
class AssetTypeTest < Test::Unit::TestCase
  def test_unique_name
    newObj = AssetType.new( {:name => 'test'} )
    assert newObj.save, 'Can save test the first time'
    newObj = AssetType.new( {:name => 'test'} )
    assert !newObj.save, 'Can not save a duplicate test'
    newObj = AssetType.new( {:name => 'Test'} )
    assert newObj.save, 'Can save Test vs test'
  end
end

----------

All tests pass, if it was case insensitive then the saving of 'Test'
should fail.

As a side note - how can I catch the validation failure message in my
test?

----------
actionmailer (1.1.1)
actionpack (1.10.1)
activerecord (1.12.1)
activesupport (1.2.1)
postgres-pr (0.4.0)
rails (0.14.1)
67b6389be42524fbd776e44fd35c3d7e?d=identicon&s=25 peter.j.donald (Guest)
on 2005-11-14 12:28
(Received via mailing list)
On 11/14/05, James Walker <james.rw.walker@gmail.com> wrote:
>   end
> end
>
> ----------
>
> All tests pass, if it was case insensitive then the saving of 'Test'
> should fail.
>
> As a side note - how can I catch the validation failure message in my test?

assert_equal("an asset type with that
name already exists", newObj.errors[:name])

--
Cheers,

Peter Donald
6993d6b4b6d882f421e1b45ac147f3e0?d=identicon&s=25 scott (Guest)
on 2005-11-14 13:19
(Received via mailing list)
On Nov 14, 2005, at 3:50 AM, Sushruth blah wrote:

> case
> insensitive
>

No, it really isn't.  It's really your DB that's case insensitive.
Observe:

PostgreSQL:
template1=# select 'foo' = 'Foo';
  ?column?
----------
  f

SQLite:
sqlite> select 'foo' = 'Foo';
0

are both false, while MySQL:
mysql> select 'foo' = 'Foo';
+---------------+
| 'foo' = 'Foo' |
+---------------+
|             1 |
+---------------+


--
Scott Barron
Lunchbox Software
http://lunchboxsoftware.com
http://lunchroom.lunchboxsoftware.com
http://rubyi.st
This topic is locked and can not be replied to.