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

Posted . 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.

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