Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-4ruby-on-rails-3.2

Rails, how to store percentage values in database


I have dilema with storing percentage values in database. I have model "Cost" in my database. It will belong to Vat. My Vat model will have only one field: "rate". And now my problem is which type of data use. I want to store for my Vat object values something like that:

  • if vat is 23% in my rate column we have 0.23.

Any ideas?


Solution

  • why not just store it as float and add methods in the vat model

    def rate
      self.rate * 100
    end
    
    def rate=(perc)
      self.rate = perc / 100
    end