Datetime field / params hash / ClassName.new

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs