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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:14:43+00:00 2026-06-09T02:14:43+00:00

I must be missing something basic but I keep getting validation errors: app/model/person.rb class

  • 0

I must be missing something basic but I keep getting validation errors:

app/model/person.rb

class Person < ActiveRecord::Base
  attr_accessible  :cell

  before_validation :format_cell_string

  validates :cell, :length => { :is => 10 }

  protected

    def format_cell_string
      self.cell = self.cell.gsub!(/\D/, '') if self.cell != nil
    end

end

In rails c

> bib = Person.new(cell: "1234567890")
> bib.save

leads to a ROLLBACK

bib.errors
=> #<ActiveModel::Errors:0x007fcb3cf978d8 @base=#<Person id: nil, created_at: nil, updated_at: nil, cell: nil>, @messages={:cell=>["is the wrong length (should be 10 characters)"]}>

Thinking that is could be a rails console or irb error, I also tried in my form to no avail. Trying bib = Person.new, bib.save and then bib.update_attributes(cell: “0123456789”) also doesn’t work in the console. Am I missing something! I’ve check the rails docs on validations and the rails api on model validations and tried many different things. Any thoughts? I was using rails 3.2.6 and just upgraded to rails 3.2.7. No change.

  • 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-09T02:14:44+00:00Added an answer on June 9, 2026 at 2:14 am

    gsub! modifies the string in place and returns nil if no changes were made:

    "1234567890".gsub!(/\D/, '') #=> nil
    

    So in the case where the field contains only digits your code is setting the field to nil before the validation which causes it to fail. Using gsub! on attributes is best avoided generally, as it doesn’t play well with Rails’ change tracking.

    self.cell = self.cell.gsub(/\D/, '') if self.cell != nil
    

    should do the trick

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

Sidebar

Related Questions

This seems very basic and I must be missing something, but here goes anyways...
I must be missing something basic here but while developing a custom jQuery widget,
I am missing something basic, here. Must have forgotten it. But basically, I have
Must be missing something really obvious here but how do you change the size
I must be missing something really obvious. I'm quite new to C# but have
I must be missing something very simple, but can't find the answer to this.
I must be missing something obvious but I'm not sure what. I've created a
I must be missing something obvious about how the various Scanner methods work but
I think i must be missing something simple here but I can't figure out
I feel like I must be missing something completely obvious, but I don't see

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.