# CRUD pattern for has\_many relationships (forms containing co

**URL:** <https://www.ruby-forum.com/t/crud-pattern-for-has-many-relationships-forms-containing-co/56953>\
**Category:** Rails\
**Created:** [April 6, 2006, 1:14am UTC](https://www.ruby-forum.com/t/crud-pattern-for-has-many-relationships-forms-containing-co/56953 "2006-04-06T01:14:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![christos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@christos](https://www.ruby-forum.com/u/christos)\
**Post date:** [April 6, 2006, 1:14am UTC](https://www.ruby-forum.com/t/crud-pattern-for-has-many-relationships-forms-containing-co/56953/1 "2006-04-06T01:14:56Z")

</div>

Hello folks!

Beare with me for a second, while I explain my problem.

Assuming we have the trivial model of

class Author  
has\_many :books  
end

class Book  
end

How do people go about creating the authors/edit and authors/new  
views if you want to be able to add and remove arbitrary amount of  
Books at Author creation and edit time? What I mean by this is that I  
go to ‘authors/new’ then add a few Books and then create the Author.

Let’s assume that I am using link\_to\_remote for the on the fly  
addition of Books to the Author. It is important that neither the  
Author nor the Books are saved to the database until the Create form  
button is pressed.

My solution so far is to use the session object to keep an Array with  
new Books that are not saved in the database.

I have a books/\_book.rhtml partial

```
<p><%= text_field_tag "books[#{book.id}][title]", book.title %></p>

```

…an authors/\_form.rhtml partial

```
<%= text_field "author", "name" %>
<%= link_to_remote "Add book", :url => {:action => "add_book"}
							    :update => "books_list" %>

<div id="books_list">
	<%= render :partial => "books/book", :collection => author.books %>
</div>

```

…and in authors\_controller.rb

```
def new
	session[:books] = Array.new
end

def add_book
	book = Book.new(:title => "untitled")
	book.id = -session[:books].size
	session[:books] << book
	render :partial => "books/book", :collection => session[:books]
end

def create
  # code ommited but..
 # I look for session[:books] and iterate through each adding a new

```

one if it has a  
# negative id, otherwise find the exisiting book (when coming from  
an edit)  
end

The above approach breaks down when adding books because book.id is  
not defined yet. The ugly hack is that I am setting the id to a  
negative value so that I can then spot it in my create method and  
create proper Book objects. The above works for the edit mode.

This gets even messier if you have Chapters and a Book has\_many  
chapters. There seems to be no recursive way of creating and then  
parsing all these.

-christos

---

<div class="post-metadata">

**Author:** ![christos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@christos](https://www.ruby-forum.com/u/christos)\
**Post date:** [April 6, 2006, 6:22am UTC](https://www.ruby-forum.com/t/crud-pattern-for-has-many-relationships-forms-containing-co/56953/2 "2006-04-06T06:22:52Z")

</div>

Hmmm. Well I’m sure exactly what your problem is, and I don’t know how  
attached you are to your links approach to adding books, but this is the  
checkbox form I have for a similar structure:

(listings habtm categories)

\<% for category in @categories %\>

 checked="checked"\<% end %\> \> \<%= h(category.name) %\>
\<% end %\>

Then in the create code I have:

@listing.categories = Category.find(params[:category\_ids])

The same can work for a book with chapters. I hope this helps.

-Adam

---

<div class="post-metadata">

**Author:** ![christos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@christos](https://www.ruby-forum.com/u/christos)\
**Post date:** [April 6, 2006, 6:23am UTC](https://www.ruby-forum.com/t/crud-pattern-for-has-many-relationships-forms-containing-co/56953/3 "2006-04-06T06:23:23Z")

</div>

That first line should read, “I’m NOT sure exactly what your problem  
is…” \>\_\>

---

<div class="post-metadata">

**Author:** ![christos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@christos](https://www.ruby-forum.com/u/christos)\
**Post date:** [April 6, 2006, 11:31am UTC](https://www.ruby-forum.com/t/crud-pattern-for-has-many-relationships-forms-containing-co/56953/4 "2006-04-06T11:31:49Z")

</div>

Hello Adam.

Maybe I didn’t explain my problem very well.

I know how to add EXISTING books to a new author at creation time,  
like you showed below.

What I need though is to be able to create one or more NEW books when  
creating a new Author. The books can have more than just one  
attribute (e.g. name, isbn, num\_of\_pages). And I need to do it  
inline in my Author form.

-christos
