Read more

ActiveRecord: scoped `validates_uniqueness_of` allows one null value per scope

Deleted user #6
August 18, 2015Software engineer

As you most likely know validates_uniqness_of :foreign_id does not allow nil values by default.

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

To allow nil one has to set the :allow_nil => true option.

Very unexpected scoping this validation will not raise an error if foreign_id set to nil for the first created record of this kind.

validates_uniqueness_of :foreign_id, :scope => :another_column # allows foreign_id to be nil

Without a validation for presence of foreign_id now unusual records could be created.

Posted to makandra dev (2015-08-18 09:23)