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 online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
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)