has_one belongs_to update woes


I'm using 2 models, Machine which has_one MachineDetail (which belongs_to Machine). Machine is a table of computers, MachineDetail has a foreign key machine_id, and is used to add some additional information about the computers. They're separate because we're maintaining a large number of tables, which independently refer to the machine table, adding additional information for different web applications.

Everything I've done in Rails so far has just worked, but now that I'm connecting these two tables together it seems the wheels are falling off a bit.

I'm looking around for a really simple way of updating both these tables at the same time. When you edit a computer, you can also edit parts of the machine_detail table.

The select is fine. I use

@machine = Machine.find(id)

and then can print @machine.details.appearance to get the joined information. This seems really nice.

With the edit form I'm running into difficulties.

The edit form has

<% form_for :machine, @machine do |f| %>    <%# ... normal machine properties ... %>

   <%# fields I want to update in the joined table: %>    <% fields_for @machine.details do |details| %>       <%= details.hidden_field :appearance %>    <% end %> <% end %>

It send POST parameters to the server that look like:




for the join table.

In my controller, I'm currently doing:

        @machine = Machine.find(params[:machine][:id], :include => 'details')         @machine.details = MachineDetail.find(:first, :conditions => ["machine_id = ?", @machine.id])         @machine.update_attributes(params[:machine])         @machine.details.update_attributes(params[:machine_detail])

Is this correct? I'd think you could do an update all in one action. It's causing my controller to look bloated and confusing.