Ruby / Rails - How can you check on a decimal scale?

How can you check the scale of a decimal number? For example, suppose we want to keep ratings for a hotel with a maximum number of 2 decimal places after the decimal point. 4.34. 3.76. etc.

I read on the web that sqlite is truncated based on the accuracy / scale that you bound to the column. Therefore, if you have accuracy 3, scale 2 and enter 1.34567 1.35 will be saved.

However, I use postgres, and it is not. I enter this and the DB stores the whole thing somehow, despite my accuracy scale of 3.

t.decimal :my_column, precision: 3, scale: 2

So, how can I check for such things and why does postgres store more than 2 decimal places?

+4
source share
2

, . , , , , , .

, ( , ), , . .

Rails , :

def my_column=(value)
  if value[/\A\d+\.\d\d\z/]
    write_attribute :my_column, value
  else
    errors.add(:my_column, :invalid) 
  end
end

, string- > decimal .

+2

100 . .

0

Source: https://habr.com/ru/post/1543687/


All Articles