Skip to content

Instantly share code, notes, and snippets.

@linyows
Created April 4, 2012 05:25
Show Gist options
  • Save linyows/2298000 to your computer and use it in GitHub Desktop.
Save linyows/2298000 to your computer and use it in GitHub Desktop.
module Mongoid
module MultiParameterAttributes
protected
# Override mongoid / lib / mongoid / multi_parameter_attributes.rb
def instantiate_object(field, values_with_empty_parameters)
return nil if values_with_empty_parameters.all? { |v| v.nil? } || !field
values = values_with_empty_parameters.collect { |v| v.nil? ? 1 : v }
klass = field.type
if klass == DateTime || klass == Date || klass == Time
field.serialize(values)
elsif klass == String
klass.new values.join
elsif klass
klass.new *values
else
values
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment