# Data from params (another solution)

**URL:** <https://www.ruby-forum.com/t/data-from-params-another-solution/112820>\
**Category:** Rails\
**Created:** [August 23, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/data-from-params-another-solution/112820 "2007-08-23T16:28:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Nurkkala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@Tom\_Nurkkala](https://www.ruby-forum.com/u/Tom_Nurkkala)\
**Post date:** [August 23, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/data-from-params-another-solution/112820/1 "2007-08-23T16:28:50Z")

</div>

There have been several postings about how to extract a :date out of  
the params hash, none of which seem to make use of Rails own  
functionality. When a model includes a :date field, the params list  
coming back from a date\_select view has multiple parts, each tagged  
with something like “(1i)”. Rails calls these multi-parameter  
attributes. When you pass the params list directly to a model’s new  
method, Rails parses the date just fine:

my\_model = MyModel.new(params[:my\_model])

However, if you want to just snag an embedded date (say,  
“entered\_on”), it seemed like you were out of luck unless you did an  
end-around and created your own Date object.

Perusing the source for ActiveRecord::Base, the initialize() method  
(called by MyObject.new and its kin) includes this line:

self.attributes = attributes unless attributes.nil?

This line invokes ActiveRecord::Base::attributes=, which includes code  
to handle multi-parameter attributes. Basically, it looks for the “(”  
in the parameter name, causing it to invoke  
assign\_multiparameter\_attributes, which does all the magic.

So the question is, how do we arrange for the multi-parameter  
attribute for a date to be assigned to our model object? Here’s what  
I came up with; I’d welcome a cleaner alternative.

my\_model.attributes = params[:my\_model].reject { |k,v| k !~ /  
^entered\_on/ }

This arranges to create a (new) hash containing just the muti-  
parameter attribute “entered\_on” (including “entered\_on(1i)”,  
“entered\_on(2i)”, etc.), and pass the hash to the attributes= method,  
which handles the date attribute.

tom.
