Validates acceptance always failing

Updated answer..

So it turns out that the problem was having terms as an actual column in the table. In general validates_acceptance_of is used without such a column, in which case it defines an attribute accessor and uses that for its validation.

In order for validates_acceptance_of to work when it maps to a real table column it is necessary to pass the :accept option, like:

validates :terms, :acceptance => {:accept => true}

The reason for this has to do with typecasting in Active Record. When the named attribute actually exists, AR performs typecasting based on the database column type. In most cases the acceptance column will be defined as a boolean and so model_object.terms will return true or false.

When there’s no such column attr_accessor :terms simply returns the value passed in to the model object from the params hash which will normally be "1" from a checkbox field.

Leave a Comment