mardi 1 novembre 2016

better way to write If statement with a lot of repeats in rails ruby

I have a table with 30 different columns and I am trying to write a method that calls on each column containing a value.

eg.

def do_something
  if self.column1.present? && self.column2.present? && .... && self.column30.present
     a whole lot of queries
     self.update_columns :column1 => :value1 * :value2
                          ......
                         :column30 => etc
  elsif self.column1.present? && self.column2.present? && .... && self.column29.present
     a whole lot of queries
     self.update_columns :column1 => :value1 * :value2
                          ......
                         :column29 => etc
 ............... end end 

This is a whole lot of repeating myself and I know ruby is dynamic so there should be a way to increment the numbers with like a for loop and it would still work. I dont know if anyone has a better way to write this

Aucun commentaire:

Enregistrer un commentaire