Reputation: 31
I have a rails model that has attributes 'term' and 'section'. I want a uniqueness constraint on these attributes, so I tried this:
validates_uniqueness_of :term, scope: :section
This would only work when both 'term' and 'scope' were not nil, but I want the constraint to still apply when there are nil values. Looking at the ActiveRecord::Validations docs, I saw that setting 'allow_nil' to false would prevent the skipping of validation when an a value is nil. But the default value for this option is already false.
I tried setting allow_nil to both false and true anyways, but there was no change in behavior. Does anyone know why I cannot get the validation to work when there are nil values?
Upvotes: 3
Views: 2109
Reputation: 63
"I want the unique constraint to be on the combination of term and section. I don't want a unique constraint on them individually."
That's cross-field validation; you'll need to build a custom validator (using validates_with). Read about that here.
Upvotes: 0