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 7541675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:57:20+00:00 2026-05-30T07:57:20+00:00

A model Country has a attribute code which is automatically converted to lowercase by

  • 0

A model Country has a attribute code which is automatically converted to lowercase by a before_save callback. Is it possible to force this behaviour on “magic” methods without rewriting large chunks of ActiveRecord::Base?

class Country < ActiveRecord::Base
  attr_accessible :code
  validates :code, :presence => true
  validates_uniqueness_of :code, :case_sensitive => false

  before_save do |country|
    country.code.downcase! unless country.code.nil?
  end
end

RSpec

describe Country do
  describe 'data normalization'
    before :each do
      @country = FactoryGirl.create(:country, :code => 'DE')
    end

    # passes
    it 'should normalize the code to lowercase on insert' do
      @country.code.should eq 'de'
    end

    # fails
    it 'should be agnostic to uppercase finds' do
      country = Country.find_by_code('DE')
      country.should_not be_nil
    end 

    # fails
    it 'should be agnostic to uppercase finds_or_creates' do
      country = Country.find_or_create_by_code('DE')
      country.id.should_not be_nil # ActiveRecord Bug?
    end
end
  • 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-05-30T07:57:22+00:00Added an answer on May 30, 2026 at 7:57 am

    This is what I came up with, altough I really hate that approach (as mentioned in the question). An easy alternative would be to set the column, table or whole database up to ignore case (but this is db dependendt).

    class Country < ActiveRecord::Base
      attr_accessible :code
      validates :code, :presence => true
      validates_uniqueness_of :code, :case_sensitive => false
    
      before_save do |country|
        country.code.downcase! unless country.code.nil?
      end
    
      class ActiveRecord::Base
        def self.method_missing_with_code_finders(method_id, *arguments, &block)
          if match = (ActiveRecord::DynamicFinderMatch.match(method_id) || ActiveRecord::DynamicScopeMatch.match(method_id))
            attribute_names = match.attribute_names
            if code_index = attribute_names.find_index('code')
              arguments[code_index].downcase!
            end
          end
          method_missing_without_code_finders(method_id, *arguments, &block)
        end
    
        class << self
          alias_method_chain(:method_missing, :code_finders)
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Address Model which has a ForeignKey to a Contact Model: class
My state model looks like this: class State < ActiveRecord::Base belongs_to :country named_scope :order_by_name,
I've defined a class called Country that has a unique name field. class Country(models.Model):
We have a Customer model, which has a lot of has_many relations, e.g. to
I have a model Country (which is the same as 'Team') and a model
My model User has a belongsTo relationship with Country . So when I add
Ok say I have the following model: class Country < ActiveRecord::Base validates_presence_of :name validates_presence_of
I have a Class which models a User and another which models his country.
This is a pretty simple question really, but let's say I'm creating a model
I've got the Restaurant and Comment models shown below. The Comment model has a

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.