Hi Steve,

This is handled by ActiveRecord usually in 
ActiveRecord::AttributeAssignment#assign_multiparameter_attributes 
(https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_assignment.rb#L62-66)

You could experiment with either mixing in that module or pulling out the 
#assign_multiparameter_attributes methods into your own module and using it 
with ActiveModel.

AFAIK there is no equivalent for ActiveModel.

— Ivan

On 08/11/2012, at 8:55 AM, Steven Ringo <[email protected]> wrote:

> Hi all,
> 
> Anyone have any idea on the best way to parse a date from a rails form and 
> coerce it into a date field in an active model object.
> 
> Rails send this in the params hash:
> 
>  "credit_card_expiry(3i)"=>"1",
>  "credit_card_expiry(2i)"=>"11",
>  "credit_card_expiry(1i)"=>"2012"
> 
> However the model sees these as being three separate attributes, and of 
> course the the active model object has no way of knowing this.
> 
> I have seen techniques that manually mangle the hash like so (or 
> introspective variants), but this feels very hacky.
> 
> params[:subscription][:credit_card_expiry] = 
> Date.new(params[:subscription][:"credit_card_expiry(1i)"].to_i,               
>                                              
> params[:subscription][:"credit_card_expiry(2i)"].to_i, 
> params[:subscription][:"credit_card_expiry(3i)"].to_i)
> 
> params[:subscription].delete(:"credit_card_expiry(1i)")
> params[:subscription].delete(:"credit_card_expiry(2i)")
> params[:subscription].delete(:"credit_card_expiry(3i)")
> 
> Thanks,
> 
> Steve
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby or Rails Oceania" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/rails-oceania/-/5N1Kll4oUZ4J.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rails-oceania?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to