# Validates\_acceptance\_of not saving to database

**URL:** <https://www.ruby-forum.com/t/validates-acceptance-of-not-saving-to-database/110990>\
**Category:** Rails\
**Created:** [August 1, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/validates-acceptance-of-not-saving-to-database/110990 "2007-08-01T19:47:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [August 1, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/validates-acceptance-of-not-saving-to-database/110990/1 "2007-08-01T19:47:59Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [August 2, 2007, 7:00am UTC](https://www.ruby-forum.com/t/validates-acceptance-of-not-saving-to-database/110990/2 "2007-08-02T07:00:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [August 3, 2007, 4:00am UTC](https://www.ruby-forum.com/t/validates-acceptance-of-not-saving-to-database/110990/3 "2007-08-03T04:00:48Z")

</div>

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.
