# Has\_many models in a form

**URL:** <https://www.ruby-forum.com/t/has-many-models-in-a-form/184389>\
**Category:** Rails\
**Created:** [March 1, 2010, 9:29pm UTC](https://www.ruby-forum.com/t/has-many-models-in-a-form/184389 "2010-03-01T21:29:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [March 1, 2010, 9:29pm UTC](https://www.ruby-forum.com/t/has-many-models-in-a-form/184389/1 "2010-03-01T21:29:38Z")

</div>

Hi, guys.  
There are four models in the program, A has\_many B through C, B has\_many  
A through C, A has\_many D, D belongs\_to A. I have a form to create A.  
Inspired by Ryan B.’ railscasts I was able to create A and D in the  
same form, and link A to B(created beforehand) through C in the same  
form as well. Is it possible to create A and B in the same form?  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [March 1, 2010, 10:01pm UTC](https://www.ruby-forum.com/t/has-many-models-in-a-form/184389/2 "2010-03-01T22:01:36Z")

</div>

On Mon, Mar 1, 2010 at 12:29 PM, Ichiro S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi, guys.  
> There are four models in the program, A has\_many B through C, B has\_many  
> A through C, A has\_many D, D belongs\_to A. I have a form to create A.  
> Inspired by Ryan B.’ railscasts I was able to create A and D in the  
> same form, and link A to B(created beforehand) through C in the same  
> form as well. Is it possible to create A and B in the same form?  
> Thanks in advance.

Ichiro, it might have been better to provide the sample code as follows:

class A \< AR  
has\_many :B, :through =\> :C  
has\_many 😃  
end

class B \< AR  
has\_many :A, :through =\> :C  
end

class D \< AR  
belongs\_to :A  
end

Next, you should be able to create A and B in the same form using  
accepts\_nested\_attributes\_for. For example,

class A \< AR  
has\_many :B, :through =\> :C  
has\_many 😃  
accepts\_nested\_attributes\_for :B  
end

Note: You’ll need to create the appropriate form as Ryan B. walks  
you through in his nested model form screencasts.

Good luck,

-Conrad

–

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [March 1, 2010, 10:33pm UTC](https://www.ruby-forum.com/t/has-many-models-in-a-form/184389/3 "2010-03-01T22:33:40Z")

</div>

Thank you for your help, Conrad. I’ll try it out later.  
You are right, I should’ve provided the sample code. It’s cleaner and  
more clear.

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [March 3, 2010, 11:12pm UTC](https://www.ruby-forum.com/t/has-many-models-in-a-form/184389/4 "2010-03-03T23:12:50Z")

</div>

Conrad T. wrote:  
Ichiro, it might have been better to provide the sample code as follows:

> class A \< AR  
> has\_many :B, :through =\> :C  
> has\_many 😃  
> end
> 
> class B \< AR  
> has\_many :A, :through =\> :C  
> end
> 
> class D \< AR  
> belongs\_to :A  
> end
> 
> Next, you should be able to create A and B in the same form using  
> accepts\_nested\_attributes\_for. For example,
> 
> class A \< AR  
> has\_many :B, :through =\> :C  
> has\_many 😃  
> accepts\_nested\_attributes\_for :B  
> end
> 
> Note: You’ll need to create the appropriate form as Ryan B. walks  
> you through in his nested model form screencasts.
> 
> Good luck,
> 
> -Conrad
> 
> –

I don’t know if I need to write a new post, but the issue is related to  
the topic. I did what Conrad suggested and it worked perfectly. But  
there’s a problem on deleting B on the edit page of A. Here’s the A.rb:

class A \< AR  
has\_many :B, :through =\> :C  
has\_many 😃  
accepts\_nested\_attributes\_for :B, :allow\_destroy=\>true  
end

When I deleted B on the edit page, B was gone, but it’s also deleted  
from table B. What I want is disassociate B from A and keep B in its  
table when I do the delete. Is there a way to do it?

Another problem is when I add B to A, if B already exists in table B,  
can I link to B instead of creating a duplicate one in table B?  
Thanks.
