activerecord on davidchua https://dchua.com/tags/activerecord/ Recent content in activerecord on davidchua Hugo -- gohugo.io en-us Thu, 06 Nov 2014 00:00:00 +0000 How to fix BigDecimal returning as Strings in Rails https://dchua.com/posts/2014-11-06-activesupport-encodes-bigdecimals-as-strings/ Thu, 06 Nov 2014 00:00:00 +0000 https://dchua.com/posts/2014-11-06-activesupport-encodes-bigdecimals-as-strings/ Something that I’ve just found out recently. Basically, Rails serializes BigDecimal objects as JSON strings because they didn’t want its values to be misintepreted when deserialized. But in Rails 4.0.x, an option to reverse that behavior was introduced in the form of: # config/application.rb ActiveSupport.encode_big_decimal_as_string = false # default = true If you’re on Rails 4.0.x, adding the above will ensure that the values returned will be numbers. If your Rails API is providing response data that contains numbers, you want to make sure that you are not accidentally encoding big decimals as string, which would cause a problem when trying to do arithmetic calculations with it. Configure legacy timestamp attributes in Activerecord https://dchua.com/posts/2014-08-19-configure-legacy-timestamp-attributes-in-activerecord/ Tue, 19 Aug 2014 00:00:00 +0000 https://dchua.com/posts/2014-08-19-configure-legacy-timestamp-attributes-in-activerecord/ In ActiveRecord, when working with legacy database and the existing database uses different fields for last_update and created_at, how do you specify the correct column names for ActiveRecord to properly record timestamps? # in your config/initializers directory, create a new file # `active_record_patch.rb` # overload module ActiveRecord module Timestamp private def timestamp_attributes_for_create #:nodoc: [:created_at, :created_on, :create_date, :post_date] end def timestamp_attributes_for_update #:nodoc: [:modtime, :updated_on, :modified_at, :updated_at, :last_update] end end end