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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:02:22+00:00 2026-05-29T04:02:22+00:00

am struggling with Ruby validates :confirmation => true in my Rails app. Consider the

  • 0

am struggling with Ruby validates :confirmation => true in my Rails app. Consider the following code:

# == Schema Information
#
# Table name: things
#
#  id            :integer         not null, primary key
#  pin           :integer(8)
#  created_at    :datetime
#  updated_at    :datetime
#

class Things < ActiveRecord::Base
  #attr_accessor :pin
  attr_accessible :pin, :pin_confirmation


  validates :pin,
            :confirmation => true,
            :length => { :within => 7..15 },
            :numericality => { :only_integer => true }

end

As the code is above, my validation works fine from the Rails console:

1.9.3-p0 :002 > l2 = Thing.create! :pin => 1234567, :pin_confirmation => 1111111
ActiveRecord::RecordInvalid: Validation failed: Pin doesn't match confirmation
    ....
1.9.3-p0 :003 > l2 = Thing.create! :pin => 1234567, :pin_confirmation => 1234567
 => #<Thing id: 2, pin: 1234567, created_at: "2012-01-30 22:03:29", updated_at: "2012-01-30 22:03:29"> 

but testing both through rspec and manually from rails server causes the validation to fail, saying they don’t match when they darn well do. If I uncomment the attr_accessor for :pin, the validations will pass but the :pin of course will not be written to the database.

I’m completely sure I’m missing something obvious and vital- just running into a brick wall.

  • 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-29T04:02:23+00:00Added an answer on May 29, 2026 at 4:02 am

    Like Frederick said above, the issue is comparing an instance of String with an instance of Integer.

    More than likely, here’s what you have in your controller:

    Thing.new(params[:thing]) # note all these params come in as a string
    

    What’s happening is that since #pin is an integer column, you will get the following behaviour:

    my_thing = Thing.new
    my_thing.pin = "123456"
    my_thing.pin  # Will be the integer 123456, the attribute has been auto-cast for you
    

    But since #pin_confirmed is just a regular attribute, not an integer column, here’s the weirdness you will see:

    my_thing = Thing.new
    my_thing.pin_confirmation = "123456"
    my_thing.pin_confirmation  # Will be the *string* "123456", the attribute has been set as is
    

    So naturally, in that case, no matter what values you have, since they come in through the “params” hash (which is always a set of strings), you will end up assigning string values to both attributes, but they will be cast to different types.

    There are several ways to fix this.

    One, you could create #pin_confirmation as an integer column in the database.

    The other is you could add an attribute setter for #pin_confirmation of the following form:

    def pin_confirmation=(val)
      @pin_confirmation = val.to_i
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Struggling with the following SQL problem. Assume a three dimensional table with entries (h,t,q)
I am a Ruby newbie and I have been struggling with the following routing
Struggling with the following problem. I have an attribute that defines the name of
im a rails (and ruby) newbie and struggling with an a scaffolded controller that
I'm struggling with send_file with rails 3.0.9 running ruby 1.9, passenger 3.0.8 on apache
I have got the code working for listing products in Ruby but am struggling
I'm struggling to get a regexp (in Ruby) that will provide the following one,
I'm using Dragonfly for handling images on a Rails 3.1 app. I'm struggling with
I am still struggling with bitwise operators using Ruby. Getting a value 11100 (28
Struggling with this tournament fixtures algorithm. The code is working perfectly but I need

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.