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

Posted Over 8 years ago. Visible to the public.

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

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.

Last edit
Over 8 years ago
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted to makandra dev (2015-08-18 07:23)