views:

282

answers:

2

The authlogic rails gem is doing a LOWER in the sql query.

SELECT * FROM `users` WHERE (LOWER(`users`.email) = '[email protected]') LIMIT 1

I want to get rid of the LOWER part since it seems to be slowing down the query by quite a bit. I'd prefer to just lower the case in the code since this query seems to be expensive.

I'm not sure where to change this behavior in authlogic.

Thanks!

+2  A: 

This comment is from lib/authlogic/acts_as_authentic/login.rb above the find_by_smart_case_login_field method:

    # This method allows you to find a record with the given login. If you notice, with ActiveRecord you have the
    # validates_uniqueness_of validation function. They give you a :case_sensitive option. I handle this in the same
    # manner that they handle that. If you are using the login field and set false for the :case_sensitive option in
    # validates_uniqueness_of_login_field_options this method will modify the query to look something like:
    #
    #   first(:conditions => ["LOWER(#{quoted_table_name}.#{login_field}) = ?", login.downcase])
    #
    # If you don't specify this it calls the good old find_by_* method:
    #
    #   find_by_login(login)
    #
    # The above also applies for using email as your login, except that you need to set the :case_sensitive in
    # validates_uniqueness_of_email_field_options to false.
    #
    # The only reason I need to do the above is for Postgres and SQLite since they perform case sensitive searches with the
    # find_by_* methods.

Are you setting case_sensitive = false in your email validation? If so, taking that out should solve this without you having to patch any code.

zetetic
I'm not setting the email validation; that's done by default in authlogic. Where would you set that?I assume something like: validates_uniqueness_of :email, :case_sensitive => :falseI tried that in my user_session model and got an undefined method for validates_uniqueness_of
bandhunt
It would be `validates_uniqueness_of_email_field_options` as per the snippet above, but if you aren't setting it in the model then that shouldn't be the cause. By default Authlogic should not be using LOWER, so it certainly seems to think that it was told to ignore case -- the question is how?
zetetic
Thanks dude!I just added validates_uniqueness_of_email_field_options :case_sensitive => trueI guess I had to explicitly add it since the default was different than what the docs said.
bandhunt
A: 

Just an extra note. The code needs to be passed in a block to acts_as_authentic

  acts_as_authentic do |c|
      c.validates_uniqueness_of_email_field_options :case_sensitive => true
  end  
bandhunt