Using associations in form helpers

I’ve got a bunch of form fields that are mostly normal:

<%= text_field ‘car’, ‘maker’, :size => 40 %>
<%= text_field ‘car’, ‘owner’, :size => 40 %>
<%= text_field ‘car’, ‘color’, :size => 40 %>

All these attributes are in the cars table. But one of the fields,
‘price’, is stored in a different table:

<%= text_field ‘car’, ‘price’, :size => 40 %>

‘price’ is an attribute of the financials table. How can I have the
form submission update the field in this second table? I tried
changing the param name to this:

<%= text_field ‘car[financials]’, ‘price’, :size => 40 %>

…but that didn’t work. I know this is weird, but the client has it
this way due to legacy reasons, and I can’t change the schema right
now. Any and all suggestions are appreciated.

Thanks!
-Jason

PS: tables and attributes have been changed to protect the innocent.

this is the easiest way i know of:

controller:

def edit_car
@car = Car.find(params[:id])
@financial = @car.financial #im assuming there is some sort of
relationship here
end

def update_car
@car = Car.find(params[:id])
@financial = @car.financial
if @car.update_attributes(params[:car])
@financial.update_attributes(params[:financial])
flash[:notice] = “car saved”
redirect_to :action => “edit_car”, :id => @car.id
else
flash[:warning] = “car not saved”
redirect_to :action => “edit_car”, :id => @car.id
end
end

view:
<%= text_field ‘car’, ‘maker’, :size => 40 %>
<%= text_field ‘car’, ‘owner’, :size => 40 %>
<%= text_field ‘car’, ‘color’, :size => 40 %>
<%= text_field ‘financial’, ‘price’, :size => 40 %>

there is probably a leaner way to do that.

–jake

Jason F. wrote:

I’ve got a bunch of form fields that are mostly normal:

<%= text_field ‘car’, ‘maker’, :size => 40 %>
<%= text_field ‘car’, ‘owner’, :size => 40 %>
<%= text_field ‘car’, ‘color’, :size => 40 %>

All these attributes are in the cars table. But one of the fields,
‘price’, is stored in a different table:

<%= text_field ‘car’, ‘price’, :size => 40 %>

‘price’ is an attribute of the financials table. How can I have the
form submission update the field in this second table? I tried
changing the param name to this:

<%= text_field ‘car[financials]’, ‘price’, :size => 40 %>

…but that didn’t work. I know this is weird, but the client has it
this way due to legacy reasons, and I can’t change the schema right
now. Any and all suggestions are appreciated.

Thanks!
-Jason

PS: tables and attributes have been changed to protect the innocent.

This is essentially the approach I am using at the moment, but I am
convinced it can be done with the right text_field names if I could
only work it out!

Something like:
CarController —
def update
if params[:car] # Make sure the form has been submitted
begin # Make sure the car exists
@car = Car.find(params[:id])
rescue
flash[:notice] = “Car not found”
redirect_to :action => "index"and return # Stop script
end
if @car.update_attributes(params[:car])
flash[:notice] = “Car details updated”
redirect_to :action => “edit_car”, :id => @car.id and return # Stop
processing the script.
else
flash[:warning] = “Car not saved”
redirect_to :action => “edit_car”, :id => @car.id
end
end

view:
<%= text_field ‘car’, ‘maker’, :size => 40 %>
<%= text_field ‘car’, ‘owner’, :size => 40 %>
<%= text_field ‘car’, ‘color’, :size => 40 %>
<%= text_field ‘car.financial’, ‘price’, :size => 40 %> # This is the
bit I have not worked out yet. It may not be possible.

Can anyone tell me if this is possible and what the text_field might
look like?

On Dec 22, 10:28 am, Jake V. [email protected]