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

On Saturday, June 7, 2014 9:17:08 PM UTC-4, Asa Romberger wrote:
>
> 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?
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/5b13751d-f387-4fdd-8ad1-72cafcd2e472%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to