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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:04:26+00:00 2026-05-12T15:04:26+00:00

The uniqueness validator of ActiveRecord has an options to skip validation if the value

  • 0

The uniqueness validator of ActiveRecord has an options to skip validation if the value is nil or blank. Even if I set both parameters to true (the default behaviour) I can create one record with nil and blank before the validation hits. I use the default SQlite3 Database sqlite3-ruby (1.2.5).

Edit for clarification: I get the expected result if I add validates_presence_of to the Model. I thought that the default behaviour of validates_uniqueness_of would make this redundant.

Testcase:

rails validation_test
cd validation_test/
script/generate Model Thing identification:string
rake db:migrate

Content of app/models/thing.rb:

class Thing < ActiveRecord::Base
  validates_uniqueness_of :identification
end

Rails console:

script/console 
Loading development environment (Rails 2.3.4)
>> Thing.create!
=> #<Thing id: 1, identification: nil, created_at: "2009-09-26 01:49:32", updated_at: "2009-09-26 01:49:32">
>> Thing.create! :identification => ""
=> #<Thing id: 2, identification: "", created_at: "2009-09-26 01:49:42", updated_at: "2009-09-26 01:49:42">
>> Thing.create! :identification => ""
ActiveRecord::RecordInvalid: Validation failed: Identification has already been taken
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:1090:in `save_without_dirty!'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/dirty.rb:87:in `save_without_transactions!'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/transactions.rb:200:in `save!'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/transactions.rb:182:in `transaction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/transactions.rb:200:in `save!'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/transactions.rb:200:in `save!'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:1059:in `create!'
    from (irb):3
>> Thing.count
=> 2

Why do the first two creations pass?

Thanks

  • 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-12T15:04:26+00:00Added an answer on May 12, 2026 at 3:04 pm

    You are mistaken about the default behavior. From the docs:

    :allow_nil – If set to true, skips this validation if the attribute is nil (default is false).
    :allow_blank – If set to true, skips this validation if the attribute is blank (default is false, it includes nil too).

    Setting allow_blank to true, I see the following behavior with Rails 2.3.4.

    class Thing < ActiveRecord::Base
      validates_uniqueness_of :identification, :allow_blank => true
    end
    
    >> Thing.create! :identification => ""
    => #<Thing id: 6, identification: "", created_at: "2009-09-26 03:09:48", updated_at: "2009-09-26 03:09:48">
    >> Thing.create! :identification => ""
    => #<Thing id: 7, identification: "", created_at: "2009-09-26 03:09:49", updated_at: "2009-09-26 03:09:49">
    >> Thing.create! :identification => nil
    => #<Thing id: 8, identification: nil, created_at: "2009-09-26 03:09:52", updated_at: "2009-09-26 03:09:52">
    >> Thing.create! :identification => nil
    => #<Thing id: 9, identification: nil, created_at: "2009-09-26 03:09:53", updated_at: "2009-09-26 03:09:53">
    

    Edit: Addressing your clarification.

    Adding a validates_presence_of would be correct for what you’re trying to do. It’s not redundant, since it’s checking for a completely different error case. It also has its own error message, which will be important for the user.

    class Thing < ActiveRecord::Base
      validates_uniqueness_of :identification, :allow_blank => true
      validates_presence_of :identification
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this validation validates :contact_id, :presence => true, :uniqueness => {:message => 'has
There are many systems that depend on the uniqueness of some particular value. Anything
i want to validate uniqueness of two filed but if second filed is nil
I have a problem with the scoped uniqueness validation in Rails. It works fine
I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
I have vote and votedown tables for posts. I can validate uniqueness within the
Is it posible to validate the uniqueness of a child model's attribute scoped against
Is there any datatype /container in any language which checks for the uniqueness of
I'm trying to validate uniqueness of some field in my model with one catch
I would like to know how to implement a validation in Core Data. What

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.