# Boolean select problem

**URL:** <https://www.ruby-forum.com/t/boolean-select-problem/79574>\
**Category:** Rails\
**Created:** [March 10, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/boolean-select-problem/79574 "2006-03-10T17:45:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 10, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/boolean-select-problem/79574/1 "2006-03-10T17:45:44Z")

</div>

I want to use select form elements to choose between boolean options.  
How  
can I have it default to true and still display the persisted value  
(either  
true or false) when editing an existing object? I guess booleans  
default to  
false by default so its not obvious how to recognize when it’s a real  
‘false’ that the user set or default false because the value was null.

thanks

---

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 10, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/boolean-select-problem/79574/2 "2006-03-10T18:01:03Z")

</div>

On Fri, 2006-03-10 at 11:43 -0500, Larry W. wrote:

> I want to use select form elements to choose between boolean options.  
> How can I have it default to true and still display the persisted  
> value (either true or false) when editing an existing object? I guess  
> booleans default to false by default so its not obvious how to  
> recognize when it’s a real ‘false’ that the user set or default false  
> because the value was null.

* * *

controller  
def new  
Animal.new  
:my\_boolean\_field = true  
end

model  
YES\_NO = [  
[“Yes”, true],  
[“No”, false]  
].freeze

view  
\<%= options = [[‘prompt?’, ‘’]] + Animal::YES\_NO  
select(“amimal”, “my\_boolean\_field”, options) %\>

Craig

---

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 10, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/boolean-select-problem/79574/3 "2006-03-10T19:01:59Z")

</div>

Maybe I’m being too literal but I’m getting an uninitialized constant  
error  
for Animal::YES\_NO. My guess is that i’m missing something obvioius in  
the  
constant declaration. It’s at the top of the model, does it need an  
explicit public declaration?

---

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 10, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/boolean-select-problem/79574/4 "2006-03-10T19:42:17Z")

</div>

it can’t see the constant. do i have to declare in the view that i’m  
using  
Animal?

---

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 10, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/boolean-select-problem/79574/5 "2006-03-10T19:23:41Z")

</div>

I was just guessing that you have a class named Animal - you would have  
to adjust for whatever class it is that you are using…

i.e. models/animal.rb  
class Animal \< ActiveRecord::Base  
def YES\_NO  
…  
end

I think I referenced the ‘Animal’ class elsewhere, you would have to  
adjust those too.

Craig

---

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 10, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/boolean-select-problem/79574/6 "2006-03-10T19:51:24Z")

</div>

I’m trying to suggest that you put it into whatever class you are using  
and substitute that class name for Animal wherever I referenced the  
Animal class. I thought by using ‘Animal’ class it would make it more  
meaningful to you.

In my case, I am in placements\_controller (and using Placement Class)

thus in my Placement class (models/placement.rb)  
YES\_NO = [  
[“Yes”, true],  
[“No”, false]  
].freeze

and in my view…

\<%= options = [[‘Accepted?’, ‘’]] + Placement::YES\_NO  
select(“placement”, “accepted”, options) %\>

(where ‘accepted’ is the boolean)

and in my placements\_controller.rb, I might have code like…

def new  
@placement = Placement.new  
@placement.accepted = true  
end

the controller code assigns a ‘default’ value of true to the ‘new’  
Placement

Craig

---

<div class="post-metadata">

**Author:** ![larrywright](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9fc348/32.png) [@larrywright](https://www.ruby-forum.com/u/larrywright)\
**Post date:** [March 10, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/boolean-select-problem/79574/7 "2006-03-10T20:00:35Z")

</div>

I did that, but it doesn’t work. I have no idea why.

I embedded the array in the view directly –  
\<%= options = [[“Yes”, true]… and that works. It’s not as clean but it  
will do for now.

Thank you very much for your help.
