user1264577
user1264577

Reputation: 124

Rails Field Validation Based on Value of Another Field

I need to validate that the value of a field (:discount) is one of an array of strings. The :discount field can also be blank UNLESS the :type field is 'FixedDeal' (STI)

validates :discount, inclusion: {in: VALID_DISCOUNTS}, allow_blank: true unless :type == 'FixedDeal'

The above code works to validation the value of :discount, but allows the field to be blank even if the type is 'FixedDeal'.

Upvotes: 0

Views: 276

Answers (1)

Dennis F. O'Connor
Dennis F. O'Connor

Reputation: 571

On your FixedDeal class add the following validation:

validates_presence_of :discount

That should work for you.

Upvotes: 1

Related Questions