#1340 ✓duplicate
bterkuile

belongs_to association does not work using a select tag selector

Reported by bterkuile | June 27th, 2010 @ 06:08 AM

Problem: belongs_to association should allow empty assignment (:include_blank => true)
Now a is not an integer error is triggered because assigning a empty string to an integer property remains an empty string.
Environment: Rails beta4, ruby 1.9.2-preview3

My solution:
Add a check for empty assignment in the numeric typecast:
config/initializers/dm-typecast-fix.rb
module DataMapper
class Property

module Typecast
  module Numeric
    # Match numeric string
    #
    # @param [#to_str, Numeric] value
    #   value to typecast
    # @param [Symbol] method
    #   method to typecast with
    #
    # @return [Numeric]
    #   number if matched, value if no match
    #
    # @api private
    def typecast_to_numeric(value, method)
      if value.respond_to?(:to_str)
        return nil if value.to_str.blank?
        if value.to_str =~ /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?|(?:\.\d+))\z/
          $1.send(method)
        else
          value
        end
      elsif value.respond_to?(method)
        value.send(method)
      else
        value
      end
    end
  end # Numeric
end # Typecast

end # Property end # DataMapper

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Pages