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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:18:36+00:00 2026-05-25T02:18:36+00:00

I want to enhance the ActiveRecord setters in Rails to ensure only valid values

  • 0

I want to enhance the ActiveRecord setters in Rails to ensure only valid values are saved. One such place where this is needed is phone numbers. A user may enter a phone number in a variety of formats such as,

(123) 456-7890
+1 123-456-7890

but I only want to store the digits and discard the rest when it goes to the database. The approach I am using right now is to override the setter methods using alias_method. Also, I am trying to put this into a module so any model class that contains phone number can include this module, and define the fields that should be cleaned up. The kind of interface I am hoping to use is,

# Person has a "phone" attribute to store phone numbers
class Person < ActiveRecord::Base
  # first include this module
  include PhoneSanitizer

  # then call the class method and tell it which 
  # fields need need to be sanitized
  sanitize_phone_field :phone
end

The only thing I’m doing inside my model classes is to include the PhoneSanitizer module (which adds a class method – sanitize_phone_field in the Person class). That method is now responsible for overriding the setter phone= method. This is the part I haven’t gotten to work.

module PhoneSanitizer

  module ClassMethods
    # wrap each of the passed-in fields with setters that
    # clean up the phone number value of non-digits.
    def sanitize_phone(*fields)
      fields.each do |field|
        new_method = "original_#{field}=".to_sym
        original_method = "#{field}=".to_sym
        alias_method new_method, original_method
        define_method(original_method) do |value|
          self.send(new_method, phone_to_number(value))
        end
      end
    end
  end

  def self.included(base)
    base.extend(ClassMethods)
  end

  def phone_to_number(number)
    number.gsub(/[^\d]/, '')
  end

end

When sanitize_phone is called, it throws an error saying :phone= is not defined for the Person class, and that makes sense. How would I go about aliasing the method for the instances of Person instead?

  • 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-25T02:18:37+00:00Added an answer on May 25, 2026 at 2:18 am

    I think your error was not undefined method alias_method it was something different and you misinterpreted it(?)

    The real problem is that the getter and setter methods on ActiveRecord are dynamic. The getter and setter methods (i.e. phone and phone=) are not created until the actual AR object is loaded from the database. At that point AR enumerates the DB fields and creates the corresponding field methods.

    These field methods are not available at the time the class is being defined in your source, so you can’t alias_method a method that does not exist. You could however do something like this instead (not tested):

    module PhoneSanitizer
      module ClassMethods
        def sanitize_phone(*fields)
          fields.each do |field|
            original_method = "#{field}=".to_sym
            define_method(original_method) do |value|
              self.write_attribute(field, phone_to_number(value))
            end
          end
        end
      end
    
      ...
    end
    

    That should accomplish pretty much the same thing as you originally intended:
    http://apidock.com/rails/ActiveRecord/AttributeMethods/Write/write_attribute

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

Sidebar

Related Questions

I want to enhance slightly a contrast of a bitmap, usually I do this
I want to use cairo to enhance gtkentry look. For this, I have connected
I am currently using the LimitedConcurrencyLevelTaskScheduler detailed here http://msdn.microsoft.com/en-us/library/ee789351.aspx I want to enhance this
I build a jquery image slider and i want to enhance it , my
I want like to enhance spam protection on my site. I've found out that
I want to enhance an application with scripting support like many other applications have,
I want to grab data from this kind of log. Nov 12 13:46:14 Home
I want to create a 3-level navigation bar like the one on http://www.bestbuy.com/ However,
I want to enhance my tool's page where as soon use click a button.
I want to enhance the functionality of acts-as-taggable-on by adding parent_id and acts_as_tree to

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.