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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:53:28+00:00 2026-05-15T22:53:28+00:00

I have a Phone model for phone numbers in my application thats built as

  • 0

I have a Phone model for phone numbers in my application thats built as follows:

t.column      :number,    :string
t.references  :phoneable, :polymorphic => true

I want to restrict the number to be of the format 317.555.5555×234, so I’m creating a form with four boxes (area code, 3 digits, 4 digits, ext):

- form_for @user do |user_form|
  -user_form.fields_for :phones do |phone|
    = phone.text_field :area_code
    = phone.text_field :first_three_digits
    etc...

I’m assuming a virtual attribute would be the route to go (a la railscasts ep16), but not sure how to assemble the “number” from the 4 separate text_fields.

I think I would have to do something like this:

def full_number=(phone)
  self.number = area_code+"."+first_three_digits+"."+second_four_digits+"."+extension
end

But I’m unsure of how to approach this in assembling the number from form inputs. Any thoughts?

  • 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-15T22:53:28+00:00Added an answer on May 15, 2026 at 10:53 pm

    I normally do this as a before_save:

    before_save :update_phone_number
    
    def update_phone_number
      self.phone_number = [area_code, first_three_digits, second_four_digits, extension].reject(&:blank?).join('.')
    end
    

    First I would have some validations:

    validates_presence_of :area_code, :first_three_digits, :second_four_digits
    validates_format_of :area_code, :with => /\d{3}/
    validates_format_of :first_three_digits, :with => /\d{3}/
    validates_format_of :second_four_digits, :with => /\d{4}/
    validates_format_of :extension, :with => /\d{0,6}/, :allow_blank => true
    

    This is just to make sure that you get valid data in your phone number and your before save doesn’t throw any errors. I also assumed that you would allow the extension to be blank, but is easily changed.

    EDIT: you will want to have attr_accessors for the different segments of the phone number:

    attr_accessor :area_code, :first_three_digits, :second_four_digits, :extension
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application in Rails 3.2.1 with a model which has a column
I have a contact model, this includes name, address, phone number, etc. I have
In my application I have some phone numbers, so that the users can call
I have to call phone number and detect if the modem at the opposite
I have the following models: class Profile(models.Model): verified = models.BooleanField(default=False) primary_phone = models.OneToOneField('Phone', related_name='is_primary',
I have phone number. Is there any way to check whether the phone number
I have a phone caller id device. I want to record calling numbers to
I have a phone number field that can have lots of different characters such
I have a phone number I want to match against a regular expression. The
If I have a phone number, and the owner of the number 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.