# Keeping DRY while creating objects with nested REST models

**URL:** <https://www.ruby-forum.com/t/keeping-dry-while-creating-objects-with-nested-rest-models/118861>\
**Category:** Rails\
**Created:** [October 31, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/keeping-dry-while-creating-objects-with-nested-rest-models/118861 "2007-10-31T18:44:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [October 31, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/keeping-dry-while-creating-objects-with-nested-rest-models/118861/1 "2007-10-31T18:44:18Z")

</div>

I have rescently jumped on the REST-wagon and am enjoying it a lot.  
Now I have run into a problem:

Lets say I have a model called Collection which has\_many Items. I know  
(from Peep Code railscast, great stuff btw) how to create new Items  
belonging to a certain Collection. In other words I can create Items  
for an existing Collection. Now I want to do this:

On the “create new collection” form I want to be able to create new  
Items without having to create the Collection first. This means the  
create action of collections\_controller needs to create new Item  
objects. This can be done within the create method but this will  
violate the DRY principle. I would like to use the create action in  
the items\_controller to create the new Items.

Grateful on any input on how to do this in an elegant way.

Kindest regards

Erik L.

---

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [November 1, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/keeping-dry-while-creating-objects-with-nested-rest-models/118861/2 "2007-11-01T13:40:46Z")

</div>

What is to stop you from just using the items\_controller to create the  
items?

---

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [November 1, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/keeping-dry-while-creating-objects-with-nested-rest-models/118861/3 "2007-11-01T16:27:26Z")

</div>

Maybe we have a similar issue, although I’m not quite sure:

Lets say I have a RESTful resource called ‘Messages’.

Each Message has associated Categories. They are also CRUDable on  
their own, so I have another RESTful resource called ‘Categories’

In my Message Controller I have a Create method, this obviously  
instantiates @message and @categories objects. The @categories object  
is essentially the same as is created by the Create method of the  
Categories controller.

Is there any way to DRY this? Is it possible to call the  
Categories#create method from the Message controller without having it  
render the Categories form? If i have render :nothing in the  
Categories#create method then I cannot create a stand alone category  
(which is necessary).

Apologies if my terminology is a bit awry…

monkeyten

---

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [November 13, 2007, 2:03am UTC](https://www.ruby-forum.com/t/keeping-dry-while-creating-objects-with-nested-rest-models/118861/4 "2007-11-13T02:03:52Z")

</div>

Your situation seems very similar to mine. Actually I found a great  
answer to this in the #73-75 screencasts at [http://railscasts.com/](http://railscasts.com/).  
Have a look, it helped me a lot.

Thanks for the input.

Regards

Erik
