Skip to content

dm-rails should map empty Strings to nil, for Integer properties that allow_nil #27

Open
postmodern opened this Issue Sep 13, 2011 · 4 comments

3 participants

@postmodern
DataMapper member

I discovered an incompatibility in how dm-rails consumes empty Strings, from select/option form fields. When a select form field is left unselected, the submitted value is always an empty String. ActiveRecord would treat empty Strings as nil values, when populating attributes. dm-rails should not map all empty Strings to nil, but only for Integer properties that allows nil.

@postmodern
DataMapper member

There is what I ended up doing for workaround this issue:

module DataMapper
  class Property
    class Integer

      def typecast_to_primitive(value)
        # filters out empty Strings for properties that allow nil
        unless (allow_nil? && value.blank?)
          typecast_to_numeric(value,:to_i)
        end
      end

    end
  end
end
@tpitale
DataMapper member
tpitale commented May 19, 2012

Will this work the same on belongs_to associations with :required => false where the the *_id property is set to an empty string?

@nibua-r
nibua-r commented May 21, 2012

@tpitale Yes. I use the following code in one of my form:
<%= select("foo", "bar_id", Bar.all.collect {|r| [ r.label, r.id ] }, { :include_blank => true }) %>

@tpitale
DataMapper member
tpitale commented Feb 7, 2016

@postmodern I'm assuming this is still an issue. If it's not something we want to solve on dm-core, do you think I should add your patch to dm-rails?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.