ruby on rails - How to assign a value to a column by using String -


i have method this:

class myclass < activerecord::base   def assign_weighted_values     unless foo.nil?       self.weighted_foo = 3 * foo     end     unless bar.nil?       self.weighted_bar = 3 * bar     end     unless hoge.nil?       self.weighted_hoge = 3 * hoge     end   end end 

but want write like:

  def assign_weighted_values     %w(foo bar hoge).each |column|       next if send(column).nil?       self.send("weighted_#{column}") = 3 * column     end   end 

is there way assign value column using string?

you can use assign_attributes

def assign_weighted_values   %(foo bar hoge).each |column|     next if send(column).nil?     assign_attributes({ "weighted_#{column}" => 3 * column })   end end 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -