Validates_acceptance_of not saving to database

Hi All,

For some reason, when I have validates_acceptance_of on a checkbox, it
works for validation, but it isn’t saving the ‘1’ (indicating
acceptance) into the field of the database - it leaves it as NULL.

If I take the validation off, it saves it as a ‘1’.

I am validating a simple check box for a legal agreement, and we have to
save in the database that they accepted the agreement…

My validation:
(in the model):
validates_acceptance_of :legal_agreement

The view:
<% form_for :enrollment, :url => { :action => ‘validate’, :id =>
params[:id] } do |f| %>
##bunch of form helpers

<%= f.check_box :legal_agreement %> Legal Acceptance / Signature

<% end %>

Any idears?

Thanks!

Dustin A. wrote:

Hi All,

For some reason, when I have validates_acceptance_of on a checkbox, it
works for validation, but it isn’t saving the ‘1’ (indicating
acceptance) into the field of the database - it leaves it as NULL.

If I take the validation off, it saves it as a ‘1’.

I am validating a simple check box for a legal agreement, and we have to
save in the database that they accepted the agreement…

My validation:
(in the model):
validates_acceptance_of :legal_agreement

The documentation only says that :terms_of_service is a virtual
attribute and is not saved to the database. But your attribute is not
called that, it’s called legal_agreement.

I just tried it myself, and it seems that when validates_acceptance_of
is used with any column, a NULL is inserted into that column (which
violates my not null constraint). Must be a bug?

validates_acceptance_of is for virtual (non-database-backed) attributes.
If
you want a database record of the agreement having been accepted, just
make
legal_agreement a boolean database field and then validate that
legal_agreement == 1.

From the Rails API:

Encapsulates the pattern of wanting to validate the acceptance of a
terms of
service check box (or similar agreement). Example:

class Person < ActiveRecord::Base
validates_acceptance_of :terms_of_service
validates_acceptance_of :eula, :message => “must be abided”
end

The terms_of_service attribute is entirely virtual. No database column
is
needed. This check is performed only if terms_of_service is not nil and
by
default on save.