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 Bates'' 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Mar 1, 2010 at 12:29 PM, Ichiro Saga <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 Bates'' 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 :D 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 :D accepts_nested_attributes_for :B end Note: You''ll need to create the appropriate form as Ryan Bates walks you through in his nested model form screencasts. Good luck, -Conrad --> Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Conrad Taylor wrote: Ichiro, it might have been better to provide the sample code as follows:> > class A < AR > has_many :B, :through => :C > has_many :D > 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 :D > accepts_nested_attributes_for :B > end > > Note: You''ll need to create the appropriate form as Ryan Bates 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 :D 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Seemingly Similar Threads
- accepts_nested_attributes_for with has_many => :through
- validates presence of foreign key fails in nested form
- how to use activemodel collection.build for a has_many :through association
- How to use group in nested associations
- 2.3 Nested Model Forms; build new without updating existing