Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8550329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:55:35+00:00 2026-06-11T13:55:35+00:00

I created a rails model class for an existing table. Some of the table

  • 0

I created a rails model class for an existing table. Some of the table fields are decimal fields defined in the Postgres DB as numeric(19,2). When I open the rails console and fetch one of this objects like this:

 ExistingTableModel.first.total
 #<BigDecimal:4bfd250,'0.692E3',9(18)>

So, I am getting a BigDecimal 9(18) . This is a problem because when I do sums and stuff like that, the results are not nice due to rounding.

How can I force rails to map those fields to a BigDecimal with 2 decimals?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:55:36+00:00Added an answer on June 11, 2026 at 1:55 pm

    You could override attribute accessor(s). Say you have a legacy Postgres table with an attribute foo numeric(19,2)

    model ExistingTableModel < ActiveRecord::Base
      def foo
        read_attribute(:foo).round(2)
      end
    end
    

    This would make every access of the foo attribute rounded to precision of 2 decimal places.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

should a non-database data class I need be created as a Rails 3 model,
I have the old legacy table called DXFTACCTS, and I created Rails model Account.
I have many models created with the standard rails model generator. Some of the
I created a rails model by doing script/generate model Customer name:string address:string city:string state:string
I have created model, controller and view with rails scaffold generator: rails g scaffold
I'm developing a booking system in Rails 3.1. I have created a model for
I'm trying to do the following in my Rails 3 model: require 'securerandom' class
hi I have two rails model class Feedback < ActiveRecord::Base has_many :conversations, :dependent =>
To start, I'm a pretty new to Rails I've created some methods and put
Im using Rails 3.0.3 I have a Model Bill: class Bill < ActiveRecord::Base has_many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.