Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider including DataMapper hack to coerce empty Strings to nil, for Integer properties #916

Closed
postmodern opened this issue Aug 23, 2012 · 8 comments

Comments

@postmodern
Copy link
Contributor

When submitting forms that include foreign-key values (foo_id=1), DataMapper is unable to coerce an empty String (the default value of a form field) to nil. Instead, DataMapper complains that the foreign-key must be nil or an Integer. As a work-around, I use this monkey-patch to DataMapper::Property::Integer:

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

See also: datamapper/dm-rails#27

@jasonseney
Copy link

Agreed, this really caused me a headache when trying to implement optional associations.

@ujifgc
Copy link
Member

ujifgc commented Jan 12, 2013

typecast_to_primitive is removed from DataMapper 1.3.

@dariocravero
Copy link

dm-core said it's not a core issue and they were going to fix that on dm-rails but it looks like nothing has happened with it. Perhaps we should ask there again?

@ujifgc
Copy link
Member

ujifgc commented Jan 12, 2013

Are we protected from mass assignment in any way? These issues should be resolved together.

@ujifgc
Copy link
Member

ujifgc commented Jan 12, 2013

Should we plan this for 1.0 or close as #905 is there to protect from mass assignment. I think disallowed params should be deleted and good ones cast to integers or nils.

@dariocravero
Copy link

Good call @ujifgc. Rescheduled it for 1.0.

@ujifgc
Copy link
Member

ujifgc commented Aug 4, 2014

Closing in favor of #1524.

Also you can use params protection to typecast foreign keys:

post :update, :params => [:name, :foreign_model_id => Integer] do

@ujifgc ujifgc closed this as completed Aug 4, 2014
@postmodern
Copy link
Contributor Author

@ujifgc too cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants