# Implementation of a decision tree with radio buttons

**URL:** <https://www.ruby-forum.com/t/implementation-of-a-decision-tree-with-radio-buttons/128538>\
**Category:** Rails\
**Created:** [February 8, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/implementation-of-a-decision-tree-with-radio-buttons/128538 "2008-02-08T15:50:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![annaking](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e274bd/32.png) [@annaking](https://www.ruby-forum.com/u/annaking)\
**Post date:** [February 8, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/implementation-of-a-decision-tree-with-radio-buttons/128538/1 "2008-02-08T15:50:35Z")

</div>

Hi all!  
I have been using ruby on rails recently to create a data entry form  
coupled with a database for archaelogical findings in Greece. Until now  
I have managed to solve all minor problems through serching the web. I  
am now stuck and I seek your expert advice ergently!

In the form of an “item”, I have a set of 6 radio buttons which work  
like a decision tree. Let’s call them r1 to r6, for simplicity.  
Checking r1 (from the pair of r1, r2) then either r3 or r4 pair can be  
checked. If r3 is checked, then r5 or r6 pair can be checked.

However, if r4 is checked, then r5 and r6 must be both unchecked, the  
same for r2.  
If r2 is checked, then the user must not be allowed to check any of r3  
to r6. In c for instance this could be solved in series of  
“if-then-else” statements.

The pseudocode could be as follows:  
if r1checked then  
r3 or r4 can be checked  
if r3 checked then  
r5 or r6 can be checked  
r2 can not be checked  
else if r4 checked  
r5 and r6 can not be checked  
end  
elseif r2 checked  
r3, r4, r5,r6 can not be checked  
end

The values for the 6 radio buttons are stored in the database in nested  
lookup tables, ie. the values of pair (r1, r2) are in lup1 table, pair  
(r3,r4) in lup2 table and (r5,r6) in lup3 table, while foreing key fk3  
connects lup3 to lup2 , fk2 connects lup2 to lup1 table, and finally fk1  
connects lup1 table to an “items” table, where other data of an item are  
stored.

I have created a scaffold called item and models for lup1, lup2 and lup3  
tables with the proper belong\_to and have\_one relationships.

How can I implement the restrictions I mentioned above in the form.rhtml  
used to add a new item or edit it ?  
I include part of the relevant code and I hope somebody can advise:

(Pattern refers to lu1 table  
Scheme refers to lu2 table  
Detail refers to lu3 table)

\<% Pattern.find(:all).each do |p| %\>   
\<%= p.name%\>\<%=radio\_button\_tag(“item[pattern\_id]”,  
p.id,@item.pattern\_id == p.id) %\>  
\<% end %\>  
\<% Scheme.find(:all).each do |s| %\>   
\<%=s.name%\>\<%=radio\_button\_tag(“p.scheme\_id”, s.id, p.scheme\_id ==  
s.id) %\>  
\<%end%\>

\<% Detail.find(:all).each do |d| %\>   
\<%=d.name%\>\<%=radio\_button\_tag(“s.detail\_id”, d.id, s.detail\_id ==  
d.id) %\>  
\<%end%\>
