Skip to content

Instantly share code, notes, and snippets.

@swistak35
Created March 20, 2013 22:35
Show Gist options
  • Save swistak35/5209173 to your computer and use it in GitHub Desktop.
Save swistak35/5209173 to your computer and use it in GitHub Desktop.
Cutting decimals observer
def before_save(model)
model.class.columns.each do |column|
if column.type == :decimal
int_part, frac_part = model.send(column.name).to_s.split(".")
new_value = [int_part, frac_part[0...column.scale]].join(".")
model.send("#{column.name}=", new_value)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment