I came across an interesting problem in one of my ActiveRecord models (paraphrased, this isn’t the exact model):

class Event < ActiveRecord::Base
  attr_accessible :certainty
  validates :certainty, :inclusion => {
    :in => %w(less neutral more),
    :message => "%{value}"

The problem is I would set certainty to one of the accepted values, let’s say 'less', and the form would wind up throwing an error. I overrode the default error message just to retrieve the value and it turns out the value for certainty is 0.

The reason this was happening is because certainty is defined as a PostgreSQL enumerated type:

  CREATE TYPE certainty AS ENUM ('less', 'neutral', 'more');

and these are the type detection methods in ActiveRecord:

From lib/active_record/connection_adapters/postgresql_adapter.rb:

def simplified_type(field_type)
  case field_type
  # Numeric and monetary types
  when /^(?:real|double precision)$/
  # Monetary types
  when 'money'
  when 'hstore'
  # Network address types
  when 'inet'
  when 'cidr'
  when 'macaddr'
  # Character types
  when /^(?:character varying|bpchar)(?:\(\d+\))?$/
  # Binary data types
  when 'bytea'
  # Date/time types
  when /^timestamp with(?:out)? time zone$/
  when 'interval'
  # Geometric types
  when /^(?:point|line|lseg|box|"?path"?|polygon|circle)$/
  # Bit strings
  when /^bit(?: varying)?(?:\(\d+\))?$/
  # XML type
  when 'xml'
  # tsvector type
  when 'tsvector'
  # Arrays
  when /^\D+\[\]$/
  # Object identifier types
  when 'oid'
  # UUID type
  when 'uuid'
  # Small and big integer types
  when /^(?:small|big)int$/
  # Pass through all types that are not specific to PostgreSQL.

and lib/active_record/connection_adapters/column.rb:

def simplified_type(field_type)
  case field_type
  when /int/i
  when /float|double/i
  when /decimal|numeric|number/i
    extract_scale(field_type) == 0 ? :integer : :decimal
  when /datetime/i
  when /timestamp/i
  when /time/i
  when /date/i
  when /clob/i, /text/i
  when /blob/i, /binary/i
  when /char/i, /string/i
  when /boolean/i

The field_type in the above method calls is 'certainty.' The simplified_type in PostgreSQLColumn doesn’t match on any of the cases and it gets passed to the parent. In the parent, Column, 'certainty' matches against 'when /int/i' and returns integer as a type. Once the integer is set as the type, ActiveRecord does its thing and converts the attribute to an integer prior to a save. The validation then triggers on the changed value.

I couldn’t figure out where the variable actually gets type checked and changed to the correct type value. I tried modifying the column type directly in the model but that didn’t prevent the variable from being converted. My solution was to simply ALTER TYPE certainty RENAME TO certain. Postgre cascaded the changes and everything ended up being okay.

I want to thank Joe for taking the lead and filing a bug report here: https://github.com/rails/rails/issues/7814.