Validating a boolean non-database addition to a model

64 views
Skip to first unread message

Asa Romberger

unread,
Jun 7, 2014, 9:17:08 PM6/7/14
to rubyonra...@googlegroups.com
I have a model with a boolean variable in the database and one added by attr_accessor:

In the model:

  attr_accessor :attrvalue

  validates(:dbvalue, inclusion: { in: [true, false], message: "%{value} is not a valid response"} )

  validates(:attrvalue, inclusion: { in: [true, false], message: "%{value} is not a valid response"} )

In the view:

    <%= form_for(@user) do |f| %>

      <%= f.select :dbvalue, {'' => nil, 'Yes' => true, 'No' => false}, {},  { :class => 'span1' } %>

      <%= f.select :attrvalue, {'' => nil, 'Yes' => true, 'No' => false}, {},  { :class => 'span1' } %>

    <% end %>

The dbvalue works, the attrvalue does not work and always throws the message attrvalue is not a valid response.

I assume that attrvalue is not a boolean. Can I force it to be a boolean? Alternately, is there another way to handle it?

Frederick Cheung

unread,
Jun 8, 2014, 7:23:09 AM6/8/14
to rubyonra...@googlegroups.com
On Sunday, June 8, 2014 2:17:08 AM UTC+1, Asa Romberger wrote:

>   attr_accessor :attrvalue

> The dbvalue works, the attrvalue does not work and always throws the message attrvalue is not a valid response.
>
> I assume that attrvalue is not a boolean. Can I force it to be a boolean? Alternately, is there another way to handle it?

Forms always submit strings, so the value that ends up in your model is the string "true" rather than he boolean true. For the dbvalue the accessors activerecord provides know that the corresponding column is a boolean column so they convert the string to the appropriate boolean (see https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/column.rb for some of the typecasting )

You could either choose to do the same (ie provide your own setter method that does this conversion) or choose to mangle the parameters in the controller. I find both options a little icky - the model is the one that knows which attribute should be of each type but it feels a little dirty that a detail of the way in which http form posts are handled (values are always strings) ends up in your model.

Fred.

James Kwame

unread,
Jun 8, 2014, 9:39:34 AM6/8/14
to rubyonra...@googlegroups.com
What exactly are you trying to achieve with this approach, why do you need to have the second attr not in the database and validated?

To answer your question, yes you can force it to be a boolean, by creating your own accessor methods

Instead of attr_accessor :attrvalue
  def attrvalue
    @value
  end

  def attrvalue=(new_value)
      if new_value == "true"
         @value = true
      else
         @value = false
      end
  end

Asa Romberger

unread,
Jun 8, 2014, 5:40:20 PM6/8/14
to rubyonra...@googlegroups.com
The second attr is a transient only used when the user signs up for an account, so I did not want to save it it in the database.

I now have in the model:

attr_accessor :completed_cfdym
def completed_cfdym
  @completed_cfdym
end
def completed_cfdym=(var)
  if var = 'true'
    @completed_cfdym = 'true'
  else
    if var = 'false'
      @completed_cfdym = 'false'
    else
      @completed_cfdym = ''
    end
  end
end
validates(:completed_cfdym, inclusion: { in: [true, false], message: "%{value} is not a valid response"} )

The '' is because I do want force the user to answer.

And the view:

      <%= f.label :completed_cfdym, "Have you already completed 'A Conversation for the Difference You Make in Life?", class: 'span5'  %>
      <%= f.select :completed_cfdym, {'' => nil, 'Yes' => true, 'No' => false}, {}, { :class => 'span1' } %>

This still fails with:
Completed cfdym is not a valid response

It also does not preserve the value that I set when it displays the error and goes back to the '' => 'nil' setting

dasibre

unread,
Jun 9, 2014, 11:13:27 AM6/9/14
to rubyonra...@googlegroups.com
You should remove the quotes from your boolean values. 
   @completed_cfdym = 'true' should be    @completed_cfdym = true this should solve the Completed cfdym is not a valid response error.
To preserve form values after failure you have to use params[:fieldname] 

<%= f.select :completed_cfdym, params[:completed_dfdym],{'' => nil, 'Yes' => true, 'No' => false}, {}, { :class => 'span1' } %>
Then use render in your controller on form submit failure.
   
    attr_accessor :

Asa Romberger

unread,
Jun 9, 2014, 3:34:19 PM6/9/14
to rubyonra...@googlegroups.com
Removing the '' on true and false made no difference.

I also tried the addition of params[:completed_cfdym], both with and without the :completed_cfdym, and both give me a rails error.

Frederick Cheung

unread,
Jun 10, 2014, 6:12:33 AM6/10/14
to rubyonra...@googlegroups.com


On Monday, June 9, 2014 8:34:19 PM UTC+1, Asa Romberger wrote:
Removing the '' on true and false made no difference.

You're comparing with = rather than ==

Fred 

Asa Romberger

unread,
Jun 10, 2014, 9:45:41 PM6/10/14
to rubyonra...@googlegroups.com
I now have:


model:

        attr_accessor :completed_cfdym
        def completed_cfdym
          @completed_cfdym
        end
        def completed_cfdym=(var)
          if var == 'true'
            @completed_cfdym = true
          else
            if var == 'false'

              @completed_cfdym = false
            else
              @completed_cfdym = ''
            end
          end
        end
        validates(:completed_cfdym, inclusion: { in: [true, false], message: "%{value} is not a valid response"} )

view:

      <%= f.label :completed_cfdym, "Have you already completed 'A Conversation for the Difference You Make in Life?", class: 'span5'  %>
      <%= f.select :completed_cfdym, {'' => nil, 'Yes' => true, 'No' => false}, {}, { :class => 'span1' } %>

I get the diagnostic:
* Completed cfdym is not a valid response

Is there a construct, like the %{value} that I can use to print out the response?
Reply all
Reply to author
Forward
0 new messages