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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Sat, Jan 9, 2010 at 6:19 PM, Quee WM <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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? > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sat, Jan 9, 2010 at 4:49 AM, Quee WM <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Conrad Taylor 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, > > -ConradThanks 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. --0022158df84f02170c047cbe327d Content-Type: text/plain; charset=ISO-8859-1 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --0022158df84f02170c047cbe327d--