Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

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
Milestone

Comments

@postmodern
Copy link
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
Copy link
Member Author

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
Copy link
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?

@r3n4ud
Copy link

r3n4ud 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
Copy link
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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants