Forum: Ruby on Rails validates_acceptance_of not saving to database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9a71afcb133b3c1a779a63835370582f?d=identicon&s=25 Dustin Anderson (dkanders)
on 2007-08-01 19:47
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
<p><%= f.check_box :legal_agreement %>
<label for="enrollment_legal_agreement">Legal Acceptance /
Signature</label></p>

<% end %>

Any idears?

Thanks!
0097752cedcb6172fc967e850d31ff3f?d=identicon&s=25 Dondi Stroma (dmspilot00)
on 2007-08-02 07:00
Dustin Anderson 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?
6122ae139470e3776aa36316296f87a3?d=identicon&s=25 Cynthia Kiser (Guest)
on 2007-08-03 04:00
(Received via mailing list)
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.
This topic is locked and can not be replied to.