Skip to content

default value conversion should support strings #325

Closed

Description

@catmando

It is legal in the rails schema to say

t.decimal "total_cost_of_failure", precision: 10, scale: 2, default: "0.0", null: false

for example. In this case default value of '0.0' should be converted to the number 0.0.

This applies to decimals, floats, integers, and booleans.

Currently the code just returns the default value without conversion (if it happens to be a string)

This is the patch needed to accomplish this:

module ReactiveRecord
  class Base
    class DummyValue < BasicObject
      def build_default_value_for_float
        return Float(0.0) unless @column_hash[:default]

        @column_hash[:default].to_f
      end

      alias build_default_value_for_decimal build_default_value_for_float

      def build_default_value_for_integer
        return Integer(0) unless @column_hash[:default]

        @column_hash[:default].to_i
      end

      alias build_default_value_for_bigint build_default_value_for_integer

      def build_default_value_for_boolean
        return false unless @column_hash[:default]

        ![false, 0, "0", "f", "F", "false", "FALSE", "off", "OFF"].include?(@column_hash[:default])
      end
    end
  end
end

Activity

  1. modified the milestone: alpha1.5 on Nov 16, 2020
  2. added a commit that references this issue on Nov 17, 2020
    215836b
  3. added
    ready-to-releaseInternal Use Only: Has been fixed, specs passing and pushed to edge branch
    on Mar 15, 2021
  4. added a commit that references this issue on Sep 2, 2026
    614bd16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomersready-to-releaseInternal Use Only: Has been fixed, specs passing and pushed to edge branch

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions