# Reducing the number of if statements

**URL:** <https://www.ruby-forum.com/t/reducing-the-number-of-if-statements/181315>\
**Category:** Rails\
**Created:** [January 9, 2010, 1:49pm UTC](https://www.ruby-forum.com/t/reducing-the-number-of-if-statements/181315 "2010-01-09T13:49:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![qaswm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5f8ce5/32.png) [@qaswm](https://www.ruby-forum.com/u/qaswm)\
**Post date:** [January 9, 2010, 1:49pm UTC](https://www.ruby-forum.com/t/reducing-the-number-of-if-statements/181315/1 "2010-01-09T13:49:56Z")

</div>

I have 34 boolean columns in my table related to a product.

I need to present this information in a table format with the column  
header and the corresponding value set to yes or no depending on the  
boolean value. The end product (i.e. the resulting html code) will be  
sent as CDATA in an xml feed. and the same process will have to be  
repeated again for each product in the database table.

What is the best way to test and then add the correct value without  
having to write 34 different if statements?

---

<div class="post-metadata">

**Author:** ![qaswm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5f8ce5/32.png) [@qaswm](https://www.ruby-forum.com/u/qaswm)\
**Post date:** [January 9, 2010, 3:42pm UTC](https://www.ruby-forum.com/t/reducing-the-number-of-if-statements/181315/2 "2010-01-09T15:42:39Z")

</div>

On Sat, Jan 9, 2010 at 4:49 AM, Quee WM [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have 34 boolean columns in my table related to a product.

I need to present this information in a table format with the column

> header and the corresponding value set to yes or no depending on the  
> boolean value. The end product (i.e. the resulting html code) will be  
> sent as CDATA in an xml feed. and the same process will have to be  
> repeated again for each product in the database table.

What is the best way to test and then add the correct value without

> having to write 34 different if statements?

You can convert the 34 boolean columns to a single binary column. Then  
you should be able to store, update, and retrieve the individual bits.  
You  
might be able to find the following screencast useful:

> **[\#189 Embedded Association - RailsCasts](http://railscasts.com/episodes/189-embedded-association)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![qaswm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5f8ce5/32.png) [@qaswm](https://www.ruby-forum.com/u/qaswm)\
**Post date:** [January 9, 2010, 5:53pm UTC](https://www.ruby-forum.com/t/reducing-the-number-of-if-statements/181315/3 "2010-01-09T17:53:33Z")

</div>

Conrad T. wrote:

> You can convert the 34 boolean columns to a single binary column. Then  
> you should be able to store, update, and retrieve the individual bits.  
> You  
> might be able to find the following screencast useful:
> 
> [#189 Embedded Association - RailsCasts](http://railscasts.com/episodes/189-embedded-association)
> 
> Good luck,
> 
> -Conrad

Thanks for the link, this does give me a better way to store the data,  
and if i am not wrong then i can use the roles array and maybe a for  
loop to test of that role is true and then append to the string?

Please correct me if i am wrong.

Regards,

Q

---

<div class="post-metadata">

**Author:** ![qaswm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5f8ce5/32.png) [@qaswm](https://www.ruby-forum.com/u/qaswm)\
**Post date:** [January 9, 2010, 10:07pm UTC](https://www.ruby-forum.com/t/reducing-the-number-of-if-statements/181315/4 "2010-01-09T22:07:12Z")

</div>

You can add the attributes to an array and then iterate over the same.  
In the loop use Object.attribute\_present?(attribute\_from\_array)  
If this returns a true u have the value set for the given attribute for  
that  
object, else u update it.

eg :

A model book have following attributes : name, author, publish\_date

Make an array : BOOKATTRIBUTES = [‘name’, ‘author’, ‘publish\_date’]

Use the following loop:

BOOKATTRIBUTES.each do |attr|

```
if @book.attribute_present?(attr)
    #don't set anything its already set.
else
    # set the value of the attribute for the object
end

```

end

---

<div class="post-metadata">

**Author:** ![qaswm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5f8ce5/32.png) [@qaswm](https://www.ruby-forum.com/u/qaswm)\
**Post date:** [January 9, 2010, 5:57pm UTC](https://www.ruby-forum.com/t/reducing-the-number-of-if-statements/181315/5 "2010-01-09T17:57:10Z")

</div>

Quee WM wrote:

> I have 34 boolean columns in my table related to a product.
> 
> I need to present this information in a table format with the column  
> header and the corresponding value set to yes or no depending on the  
> boolean value. The end product (i.e. the resulting html code) will be  
> sent as CDATA in an xml feed. and the same process will have to be  
> repeated again for each product in the database table.
> 
> What is the best way to test and then add the correct value without  
> having to write 34 different if statements?

Override Boolean#to\_s so that it produces the desired output.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
