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

  • Home
  • SEARCH
  • 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 8634055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:37:30+00:00 2026-06-12T09:37:30+00:00

I have a form with a text_field tag for phone numbers, which I would

  • 0

I have a form with a text_field tag for phone numbers, which I would like to save the information to the database as a pure integer. This I though would involve a before_save that would essentially remove all the (), – etc.

I found in a different post that something like this could overwrite the value that comes in from the field “phone”.

def original_number=(value)
  value.gsub!(/\D/, '') if num.is_a?(String)
  write_attribute(:original_number, num.to_i)
end

But, the =(value) is confusing me a bit. How would I write that as a before_save parameter?

i.e before_save #######

def #######

end

How would I write that for original_number=(value) ?

Here’s what my model looks like so far

# == Schema Information
#
# Table name: phones
#
#  id              :integer          not null, primary key
#  name            :string(255)
#  twilio_number   :integer
#  original_number :integer
#  user_id         :integer
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#

class Phone < ActiveRecord::Base

  attr_accessible :original_number, :user_id, :name, :twilio_number
  belongs_to :user

  validates :name, presence: true
  validates :twilio_number, presence: true
  validates :original_number, presence: true
  validates :user_id, presence: true

  default_scope order: 'phones.created_at DESC'

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-06-12T09:37:35+00:00Added an answer on June 12, 2026 at 9:37 am

    By defining def original_number=(value) method you are overriding the default accessor method. So you wouldnt need to do anything on before_save.
    Although the code should look like

    def original_number=(value)
      num = value.to_s.gsub(/[^0-9+]/, "")
      write_attribute(:original_number, num.to_i)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have form which is built like this: <%= form_for @location do |f| %>
I have a form which takes users information and stores it in the database.
I have a form like this <%= form_tag organizations_add_user_path, :method => :put do %>
Say I have two fields in a new or edit form: <%= f.text_field :email
I want to have a text value for a form like search website.com... but
I have a form for creating an album like following: # app/views/albums/new.html.erb <h2>Details of
In a form I have a list of products from database and every product
I have this form: in header of my website: <% form_tag request.path, :method =>
I have a custom form builder, and one of the reasons for this custom
I'd like to have a single form that has 2 or more field-set_tags and

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.