Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:09:19+00:00 2026-06-07T00:09:19+00:00

Though in general I like case-sensitivity in my database, there are a few times

  • 0

Though in general I like case-sensitivity in my database, there are a few times I find it a pain. user_name and email are good examples of fields where I don’t want to worry about the case–especially when searching.

It is easy enough to downcase a string before saving using something like:

before_save do
self.email.downcase!
end

This way the user_name and emails are always saved in lower case. But what is the best way to find_by_user_name and find_by_email? I can of course remember to always downcase the string I am passing into those methods–but that doesn’t seem very DRY.

I thought about overriding find_by_email and calling the original with super once I downcased the e-amail, but I didn’t have any luck figuring that out.

So, what is the best way to do this?

PS I don’t think related posts like this one ( how can I write a case-insensitive find_by_email for Rails 3 ) are trying to solve for the same problem. Custom sql here with “lower” would be ointless for me since I already have ensured all of those values are lower–it is the one coming in right now (probably from a form where the user entered it) which I need downcased.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:09:20+00:00Added an answer on June 7, 2026 at 12:09 am

    One of these should be good:

    def self.find_by_lower_email(email)
      User.find_by_email(email.downcase)
    end
    # OR
    def self.find_by_email(email)
      User.find(:all, :conditions => ["email = lower(?)", email]) 
    end
    

    Update:

    The find_by_… functions are actually non-existing ones. When you call one of them, the ActiveRecord::Base catches your call in the method_missing method, and if the name is well formatted (contains column names, etc) then the appropriate find method is created for the class. After that it will exist.

    If you have a find_by_email, it will be called by default. If it calls super, that means that you try to call ActiveRecord::Base.find_by_email, which does not exists. The missing_method catches it, and creates (actually overwrites your) find_by_email implementation. That is why not good to call super there. If you use your own implementation for finding by email, like I wrote in the second part, then it will work splendidly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this is a general ruby question, though in my case the objects
I'm using OpenLayers to display OpenStreetMap maps. (Though, I'd assume this should be general
though I have visited this site many times, this is my first question. After
Is there a way in C# or .NET in general to create an attribute
I'd like to remove all unchecked warnings from this general utility method (part of
Reading through some of the questions here, the general concensus seems to be that
This is kind of a tough thing to search for because of how general
Though the question is generic, I would mention the scenario which sparked the query.
Though I've programming experience, am completely new to GS, JS, or anything related to
Though it's debatable, I've heard the majority of CSS developers prefer multi-line because of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.