# Can Rails deal with related attributes in forms?

**URL:** <https://rubytalk.org/t/can-rails-deal-with-related-attributes-in-forms/19563>\
**Category:** ruby-talk\
**Created:** [16 July 2005 16:00 UTC](https://rubytalk.org/t/can-rails-deal-with-related-attributes-in-forms/19563 "2005-07-16T16:00:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Jones1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Robert\_Jones1](https://rubytalk.org/u/Robert_Jones1)\
**Post date:** [16 July 2005 16:00 UTC](https://rubytalk.org/t/can-rails-deal-with-related-attributes-in-forms/19563/1 "2005-07-16T16:00:55Z")

</div>

Hi! I'm exploring whether or not to move to Rails, and the solution to the  
following problem would be a big step in the right direction for me:

Suppose that object A has many of object B, and object B has some attribute  
c, and that my Rails app has models that hold these associations.

Can Rails then help me to produce an edit form for object A that includes a  
list of all the related object B's, and allows me to edit each of their  
attribute c's directly (ie not by simply providing a link to a seperate  
edit form for each of the object B's)

I've made this happen using PHP/MySql - I'm hoping that Rails can do it 10  
times faster!

> **···**
>
> --  
> Robert Jones

---

<div class="post-metadata">

**Author:** ![Michael\_Schuerig](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Michael\_Schuerig](https://rubytalk.org/u/Michael_Schuerig)\
**Post date:** [16 July 2005 23:00 UTC](https://rubytalk.org/t/can-rails-deal-with-related-attributes-in-forms/19563/2 "2005-07-16T23:00:53Z")

</div>

Robert Jones wrote:

> Can Rails then help me to produce an edit form for object A that  
> includes a list of all the related object B's, and allows me to edit  
> each of their attribute c's directly (ie not by simply providing a  
> link to a seperate edit form for each of the object B's)

Yes, that's entirely possible.

[http://wiki.rubyonrails.com/rails/show/HowToEditObjectArraysUsingForms](http://wiki.rubyonrails.com/rails/show/HowToEditObjectArraysUsingForms)

That still looks more complicated than it should, IMHO, but I think for  
the time being there's no easier solution.

Michael

> **···**
>
> --  
> Michael Schuerig They tell you that the darkness  
> [mailto:michael@schuerig](mailto:michael@schuerig).de Is a blessing in disguise  
> [Michael Schürig | Sentenced to making sense](http://www.schuerig.de/michael/) --Janis Ian, From Me To You

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [16 July 2005 23:04 UTC](https://rubytalk.org/t/can-rails-deal-with-related-attributes-in-forms/19563/3 "2005-07-16T23:04:51Z")

</div>

This doesn't seem to be a matter of "can" Rails do it. The existing  
scaffolding may or may not be able to directly support this, but it should be  
darn simple to write something in any decent framework, with any decent ORM,  
that can.

I use a component in production that could trivially be extended to do exactly  
this, generically.

Kirk Haines

> **···**
>
> On Saturday 16 July 2005 10:00 am, Robert Jones wrote:
> 
> > Suppose that object A has many of object B, and object B has some attribute  
> > c, and that my Rails app has models that hold these associations.
> > 
> > Can Rails then help me to produce an edit form for object A that includes a  
> > list of all the related object B's, and allows me to edit each of their  
> > attribute c's directly (ie not by simply providing a link to a seperate  
> > edit form for each of the object B's)

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [17 July 2005 00:05 UTC](https://rubytalk.org/t/can-rails-deal-with-related-attributes-in-forms/19563/4 "2005-07-17T00:05:01Z")

</div>

Interesting little article.

For comparison, here's the equivalent in my current IOWA version, with  
assumption that the list of items comes from somewhere useful, such as a  
database query, and is just passed to the List component when the list is  
called:

Views:

List.html

> **···**
>
> On Saturday 16 July 2005 5:00 pm, Michael Schuerig wrote:
> 
> > Yes, that's entirely possible.
> > 
> > [http://wiki.rubyonrails.com/rails/show/HowToEditObjectArraysUsingForms](http://wiki.rubyonrails.com/rails/show/HowToEditObjectArraysUsingForms)
> > 
> > That still looks more complicated than it should, IMHO, but I think for  
> > the time being there's no easier solution.
> 
> -----  
> \<h1\>Item#list\</h1\>
> 
> \<table border="1" cellspacing="0" cellpadding="0" width="10%"\>  
> \<repeat oid="item\_list"\>\<tr\>\<td\>@item.name\</td\>\</tr\>  
> \</repeat\>  
> \</table\>  
> \<br /\>  
> \<a oid="goto\_edit"\>Edit\</a\>  
> -----
> 
> Edit.html  
> -----  
> \<h1\>Edit itemlist\</h1\>
> 
> \<form oid="save\_items"\>  
> \<table border="1" cellspacing="0" cellpadding="0"\>  
> \<repeat oid="item\_list"\>  
> &nbsp;&nbsp;\<tr\>\<td\>\<input type="text" oid="item.name" size="20"\>)\</td\>\</tr\>\</repeat\>  
> \</table\>  
> \<br /\>  
> \<input type='submit' oid="save\_items" value='Save' /\>  
> \</form\>  
> \<br /\>  
> \<a oid="go\_back"\>List\</a\>  
> -----
> 
> Controllers:
> 
> List.iwa  
> -----  
> class List \< Iowa::Component  
> &nbsp;&nbsp;attr\_accessor :item, :items
> 
> &nbsp;&nbsp;def goto\_edit  
> &nbsp;&nbsp;&nbsp;&nbsp;edit\_page = page\_named('Edit')  
> &nbsp;&nbsp;&nbsp;&nbsp;edit\_page.items = @items  
> &nbsp;&nbsp;&nbsp;&nbsp;yield edit\_page  
> &nbsp;&nbsp;end  
> end
> 
> \<?  
> &nbsp;&nbsp;item\_list {  
> &nbsp;&nbsp;&nbsp;&nbsp;item = item  
> &nbsp;&nbsp;&nbsp;&nbsp;list = items  
> &nbsp;&nbsp;}  
> ?\>  
> -----
> 
> Edit.iwa  
> -----  
> class Edit \< Iowa::Component  
> &nbsp;&nbsp;attr\_accessor :item. :items
> 
> &nbsp;&nbsp;def go\_back  
> &nbsp;&nbsp;&nbsp;&nbsp;yield prev\_page  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def save\_items  
> &nbsp;&nbsp;&nbsp;&nbsp;# This really isn't needed unless something special needs to be done; the  
> &nbsp;&nbsp;&nbsp;&nbsp;# changes automatically get saved.  
> &nbsp;&nbsp;end  
> end
> 
> \<?  
> &nbsp;&nbsp;item\_list {  
> &nbsp;&nbsp;&nbsp;&nbsp;item = item  
> &nbsp;&nbsp;&nbsp;&nbsp;list = items  
> &nbsp;&nbsp;}  
> ?\>
> 
> Note that this example above doesn't sound like it quite does what the OP was  
> asking for, which is to have a master record with it's fields, and each of  
> the associated records with their fields, all displayed in one big form for  
> editing.
> 
> Using my CRUDForm component to do that, the view would end up being something  
> like this:
> 
> -----  
> \<h1\>Edit itemlist\</h1\>
> 
> \<crudform oid="item\_record" /\>
> 
> \<br /\>  
> \<a oid="go\_back"\>List\</a\>  
> -----
> 
> And the controller something like so:  
> -----  
> import 'CRUDForm'  
> class Edit \< Iowa::Component  
> &nbsp;&nbsp;attr\_accessor :item
> 
> &nbsp;&nbsp;def go\_back  
> &nbsp;&nbsp;&nbsp;&nbsp;yield prev\_page  
> &nbsp;&nbsp;end  
> end
> 
> \<?  
> &nbsp;&nbsp;item\_record {  
> &nbsp;&nbsp;&nbsp;&nbsp;record = item  
> &nbsp;&nbsp;&nbsp;&nbsp;cascade = true  
> &nbsp;&nbsp;}  
> ?\>  
> -----
> 
> I'm sure one can do something similarly simple to use with Rails and Nitro, as  
> well.
> 
> Kirk Haines

---

<div class="post-metadata">

**Author:** ![Robert\_Jones1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Robert\_Jones1](https://rubytalk.org/u/Robert_Jones1)\
**Post date:** [17 July 2005 18:30 UTC](https://rubytalk.org/t/can-rails-deal-with-related-attributes-in-forms/19563/5 "2005-07-17T18:30:54Z")

</div>

Kirk Haines wrote:

> > Suppose that object A has many of object B, and object B has some  
> > attribute c, and that my Rails app has models that hold these  
> > associations.
> > 
> > Can Rails then help me to produce an edit form for object A that includes  
> > a list of all the related object B's, and allows me to edit each of their  
> > attribute c's directly (ie not by simply providing a link to a seperate  
> > edit form for each of the object B's)
> 
> This doesn't seem to be a matter of "can" Rails do it. The existing  
> scaffolding may or may not be able to directly support this, but it should  
> be darn simple to write something in any decent framework, with any decent  
> ORM, that can.
> 
> I use a component in production that could trivially be extended to do  
> exactly this, generically.
> 
> Kirk Haines

I made it happen, pretty easily, but I still think this ought to be built  
into Rails. Can't see why it isn't. Maybe it is and I'm missing  
something.

I used an example of houses and rooms. Each house has many rooms, and the  
rooms have names. In order to produce a form to edit the houses, which  
allows direct editing of the names of the rooms in the house, I did the  
following:

sql:

> **···**
>
> > On Saturday 16 July 2005 10:00 am, Robert Jones wrote:
> 
> -------------------------------------------------------------  
> CREATE TABLE `houses` (  
> &nbsp;&nbsp;`id` mediumint(9) NOT NULL auto\_increment,  
> &nbsp;&nbsp;`name` varchar(30) NOT NULL default '',  
> &nbsp;&nbsp;`description` text NOT NULL,  
> &nbsp;&nbsp;PRIMARY KEY (`id`)  
> ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO\_INCREMENT=3 ;
> 
> #  
> # Dumping data for table `houses`  
> #
> 
> INSERT INTO `houses` VALUES (1, 'Briary Bush', 'Lovely little house near a  
> babbling river');  
> INSERT INTO `houses` VALUES (2, 'Fawlty Towers', 'A grotty hell hole');
> 
> CREATE TABLE `rooms` (  
> &nbsp;&nbsp;`id` mediumint(9) NOT NULL auto\_increment,  
> &nbsp;&nbsp;`name` varchar(50) NOT NULL default '',  
> &nbsp;&nbsp;`house_id` mediumint(9) NOT NULL default '0',  
> &nbsp;&nbsp;PRIMARY KEY (`id`)  
> ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO\_INCREMENT=4 ;
> 
> #  
> # Dumping data for table `rooms`  
> #
> 
> INSERT INTO `rooms` VALUES (1, 'First Floor Front Room', 1);  
> INSERT INTO `rooms` VALUES (2, 'Penthouse', 1);  
> INSERT INTO `rooms` VALUES (3, 'Room 1', 2);
> 
> -------------------------------------------------------------
> 
> house.rb:
> 
> -------------------------------------------------------------  
> class House \< ActiveRecord::Base  
> &nbsp;&nbsp;has\_many :rooms  
> &nbsp;&nbsp;  
> # This extends update\_attributes to allow for the updating of the  
> associated records.  
> # The "rooms" hash is in the form {"id"=\>{"column"=\>"value",.......},  
> "id"=\>{"column"=\>"value",.....},....}  
> &nbsp;&nbsp;def update\_attributes(attributes)  
> &nbsp;&nbsp;&nbsp;&nbsp;attributes["rooms"].each do |key,value|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@room=Room.find(key)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@room.update\_attributes(value)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;attributes.delete("rooms")  
> &nbsp;&nbsp;&nbsp;&nbsp;super(attributes)  
> &nbsp;&nbsp;end  
> end
> 
> -------------------------------------------------------------
> 
> room.rb:
> 
> -------------------------------------------------------------  
> class Room \< ActiveRecord::Base  
> &nbsp;&nbsp;belongs\_to :house  
> end  
> -------------------------------------------------------------
> 
> \_form.html partial in views/houses :
> 
> -----------------------------------------------------------  
> \<%= error\_messages\_for 'house' %\>
> 
> \<!--[form:house]--\>  
> \<p\>\<label for="house\_name"\>Name\</label\>\<br/\>  
> \<%= text\_field 'house', 'name' %\>\</p\>
> 
> \<p\>\<label for="house\_description"\>Description\</label\>\<br/\>  
> \<%= text\_area 'house', 'description', "rows"=\>"5" %\>\</p\>  
> \<% for room in @house.rooms %\>  
> \<label for="room\_\<%=room.id %\>\_name"\>Room \<%=h room. id%\>\</label\>  
> \<input id="house\_rooms\_\<%=h room. id%\>\_name" name="house[rooms][\<%=h room.  
> id%\>][name]" type="text" value="\<%=h room. name%\>" /\>  
> \<% end %\>  
> \<!--[eoform:house]--\>
> 
> -------------------------------------------------------------
> 
> I look forward to someone showing me a neater way to do it 🙂
> 
> The extension of the update\_attributes method seems so simple that I can't  
> believe it would take much work to generalise this and have  
> update\_attributes deal with associated data automatically. Any takers for  
> this wee Rails development task ? 😉  
> --  
> Robert Jones
