Reducing the number of if statements

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?

On Sat, Jan 9, 2010 at 4:49 AM, Quee WM [email protected] 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:

http://railscasts.com/episodes/189-embedded-association

Good luck,

-Conrad

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:

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

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

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
[email protected]