# Check\_box:how to update 2 checkboxes dependent on each other

**URL:** <https://www.ruby-forum.com/t/check-box-how-to-update-2-checkboxes-dependent-on-each-other/62710>\
**Category:** Rails\
**Created:** [June 7, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/check-box-how-to-update-2-checkboxes-dependent-on-each-other/62710 "2006-06-07T14:44:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [June 7, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/check-box-how-to-update-2-checkboxes-dependent-on-each-other/62710/1 "2006-06-07T14:44:09Z")

</div>

I’ve got the following problem,

I’ve created two check\_boxes in my view. The user has to select one  
check\_box,  
but isn’t allowed to seth both of them together on true. Now i am  
looking for functionality that can help me doing the folling:

Scenario 1  
CheckBox1 = True, The user sets CheckBox2 from False to True, now  
CheckBox1 should automatically become False.

Scenario 2  
CheckBox2 = True, The user sets CheckBox1 from False to True, now  
CheckBox2 should automatically become False.

Is there somebody, who knows how to build a function for that?

The select\_boxes in my view look like this:

\<%= check\_box ‘account’, ‘agriculture’ %\>  
\<%= check\_box ‘account’, ‘vat\_duty’ %\>

-\> agriculture and var\_duty are both existing fields in my model.

Thnx Michel Dogger

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [June 7, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/check-box-how-to-update-2-checkboxes-dependent-on-each-other/62710/2 "2006-06-07T16:01:02Z")

</div>

Try radio buttons. These only allow one selection at a time

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [June 7, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/check-box-how-to-update-2-checkboxes-dependent-on-each-other/62710/3 "2006-06-07T16:17:57Z")

</div>

Nevermind… I get it… they’re two separate options.

You’re describing something typically done with Javascript. onclick  
functions on each button would modify the other button.

THere are things like observe\_field and other helpers in Rails that  
are available for this, but it might be pretty easy to do it yourself.

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [June 7, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/check-box-how-to-update-2-checkboxes-dependent-on-each-other/62710/4 "2006-06-07T16:44:49Z")

</div>

Still, it sounds like you should use radio buttons. It’ll be more  
accessible that way–this is what radio buttons were designed for.

Then, take care of the different variables in your model at the  
application level. Perhaps with a before\_save in your model?

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [June 7, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/check-box-how-to-update-2-checkboxes-dependent-on-each-other/62710/5 "2006-06-07T16:11:10Z")

</div>

Almost sounds like radio buttons, or am I not understanding your  
question?

True ( ) False ( )

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [June 8, 2006, 11:32am UTC](https://www.ruby-forum.com/t/check-box-how-to-update-2-checkboxes-dependent-on-each-other/62710/6 "2006-06-08T11:32:19Z")

</div>

Thnx guys,

I’ve switched over to use radio buttons, and  
that, for the moment, works fine for me.

micheldogger
