ActiveRecord 4 can't initialize custom numeric domain with "" #14305

Closed
joevandyk opened this Issue Mar 6, 2014 · 12 comments

Comments

Projects
None yet
6 participants
Contributor

joevandyk commented Mar 6, 2014

I have a custom domain like create domain custom_money numeric(8,2). Rails can't initialize that with "" (say, from a form).

joevandyk/rails@d95cef8 has a test.

Contributor

joevandyk commented Mar 14, 2014

I'll paypal the fixer of this bug $150 when it's done. :) fix needs to be usable on 4.0-stable.

Owner

jeremy commented Mar 14, 2014

💸

💸

💸

💸

💸

💸

@joevandyk have you fixed this yet?

Contributor

joevandyk commented Mar 24, 2014

Nope.

So your problem is when you create a DOMAIN with the type of numeric(8,2), rails doesn't initialise it as ' ' but rails can initialise numeric(8,2) variable normally. Right?

Contributor

joevandyk commented Mar 25, 2014

Yes.

I wrote a single file test to validate the bug: https://gist.github.com/zhuyifan2013/9777647
I will try to fix it these days.

Member

senny commented Mar 26, 2014

There are two distinct issues:

Custom domains are currently not registered in our OID type map. This changes the way type casting operates because you will get OID::Identity and not OID::Decimal.

The type detection is hardcoded on the Column. Since custom_money is not known, this will yield a column type of nil.

Following is a monkey patch tested against 4-0-stable, which addresses both issues:

module CustomMoneyDomainPatch
  def simplified_type(field_type)
    if field_type == "custom_money"
      :decimal
    else
      super
    end
  end
end

ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send :include, CustomMoneyDomainPatch
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID.tap do |klass|
  klass.register_type('custom_money', klass::Decimal.new)
end
ActiveRecord::Base.connection.send :reload_type_map

Make sure to load it before your model loads it's schema information. After this patch the test case in the gist above passes.

@joevandyk I am currently working in this area but the result will most likely not be backported. The monkey patch is probably your best bet.

EDIT: The working example can be found here https://gist.github.com/senny/9785565

@senny I think we should fix the bug within rails not within application although it indeed works.

Owner

rafaelfranca commented Mar 27, 2014

@zhuyifan2013 sure, we need to fix on Rails, but it will unlikely be backported to a stable branch so users who want the fix early will have to use master, which can lead more bugs or use the monkey patch.

@rafaelfranca you are right... good luck to him...

senny closed this in f7a6b11 Apr 1, 2014

Member

senny commented Apr 1, 2014

@joevandyk pushed a fix to master. That probably won't be backported as it relies on #14513. Is the provided workaround good enough for your current situation?

@tgxworld tgxworld added a commit to tgxworld/rails that referenced this issue Apr 3, 2014

@senny @tgxworld senny + tgxworld PostgreSQL, register custom domains. Closes #14305.
This patch registers custom domains in our OID-type_map.
They will behave exactly as the type specified by `pg_type.typbasetype`.

/cc @matthewd
d98b2c8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment