# Datetime field / params hash / ClassName.new

**URL:** <https://www.ruby-forum.com/t/datetime-field-params-hash-classname-new/121982>\
**Category:** Rails\
**Created:** [December 3, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/datetime-field-params-hash-classname-new/121982 "2007-12-03T16:53:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wolfgang\_Klinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@Wolfgang\_Klinger](https://www.ruby-forum.com/u/Wolfgang_Klinger)\
**Post date:** [December 3, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/datetime-field-params-hash-classname-new/121982/1 "2007-12-03T16:53:36Z")

</div>

_hiya!_

## I have a form field with:

## \<%= datetime\_select :ownership, :bought\_on, { :include\_blank =\> true } %\>

## and in the controller:

# ownership has owner\_id, house\_id, bought\_on (datetime)

## ownership = Ownership.new(:owner =\> @owner, :house =\> House.find (params[:house][:id])) ownership.update\_attributes(params[:ownership])

because the “bought\_on” parameter is not a single value:  
“ownership”=\>{“bought\_on(2i)”=\>“4”, “bought\_on(3i)”=\>“4”,  
“bought\_on(4i)”=\>“04”, “bought\_on(5i)”=\>“12”, “bought\_on(1i)”=\>“2003”}

How can I pass that to “new” as my solution results in  
two sql statements (insert and an unnecessary update).

tia, bye  
Wolfgang

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Klinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ce7236/32.png) [@Wolfgang\_Klinger](https://www.ruby-forum.com/u/Wolfgang_Klinger)\
**Post date:** [December 6, 2007, 3:42am UTC](https://www.ruby-forum.com/t/datetime-field-params-hash-classname-new/121982/2 "2007-12-06T03:42:32Z")

</div>

Wolfgang Klinger wrote:

> _hiya!_
> 
> ## I have a form field with:
> 
> ## \<%= datetime\_select :ownership, :bought\_on, { :include\_blank =\> true } %\>
> 
> ## and in the controller:
> 
> # ownership has owner\_id, house\_id, bought\_on (datetime)
> 
> ## ownership = Ownership.new(:owner =\> @owner, :house =\> House.find (params[:house][:id])) ownership.update\_attributes(params[:ownership])
> 
> because the “bought\_on” parameter is not a single value:  
> “ownership”=\>{“bought\_on(2i)”=\>“4”, “bought\_on(3i)”=\>“4”,  
> “bought\_on(4i)”=\>“04”, “bought\_on(5i)”=\>“12”, “bought\_on(1i)”=\>“2003”}
> 
> How can I pass that to “new” as my solution results in  
> two sql statements (insert and an unnecessary update).

Firstly, Ownership.create!(hash) is more efficient and less code.  
To get the hash you need, how about  
hash = {:owner\_id =\> @owner.id, :house\_id =\>  
params[:house][:id]}.merge!(params[:ownership]).

This two part solution makes debugging easier as you can inspect hash  
before it goes into create!()

Cheers,

magpie
