# Has\_one belongs\_to update woes

**URL:** <https://www.ruby-forum.com/t/has-one-belongs-to-update-woes/140939>\
**Category:** Rails\
**Created:** [June 19, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/has-one-belongs-to-update-woes/140939 "2008-06-19T23:22:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![luke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@luke](https://www.ruby-forum.com/u/luke)\
**Post date:** [June 19, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/has-one-belongs-to-update-woes/140939/1 "2008-06-19T23:22:54Z")

</div>

Hi,

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:

params[:machine][:serialNo]

and:

params[:machine\_detail][:appearance]

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.

Thanks,

Luke
