Hi all, I have a relationship in this form: A person belongs to an address and a address can have many persons like below: class Person < ActiveRecord::Base belongs_to :address end class Address < ActiveRecord::Base has_many: persons end The ideia behind this is, if two or more persons share the same address, isn''t necessary create two indetical rows in the database. But, like i''m new in the rails development, i want to create a form for person creation and edition that wraps the creation of related address, as a nested form. I already made a search for a solution for this problem, but the solutions that i found in others sites are for the inverse associations, any idea? 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.
You should probably have the Person have an Address, that way you can nest the Address creation form within your Person form. Logically, you shouldn''t nest the form of an Address within a Person form since it''s the Address that owns the Person according to your current model... On Apr 29, 2:18 pm, Sereno Mendes <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi all, > > I have a relationship in this form: A person belongs to an address and a > address can have many persons like below: > > class Person < ActiveRecord::Base > belongs_to :address > end > > class Address < ActiveRecord::Base > has_many: persons > end > > The ideia behind this is, if two or more persons share the same address, > isn''t necessary create two indetical rows in the database. But, like i''m > new in the rails development, i want to create a form for person > creation and edition that wraps the creation of related address, as a > nested form. I already made a search for a solution for this problem, > but the solutions that i found in others sites are for the inverse > associations, any idea? > > Thanks. > -- > Posted viahttp://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@googlegroups.com. > For more options, visit this group athttp://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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Mlle wrote:> You should probably have the Person have an Address, that way you can > nest the Address creation form within your Person form. > Logically, you shouldn''t nest the form of an Address within a Person > form since it''s the Address that owns the Person according to your > current model...No, no. I solved the issue and the creation makes sense. View this post and you will understand why. http://stackoverflow.com/questions/1313149/getting-fields-for-and-accepts-nested-attributes-for-to-work-with-a-belongs-to-re -- 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.
I''m relatively new to Rails myself, though I''ve been somewhat successful with nested model associations in both directions. Models: class Person < ActiveRecord::Base belongs_to :address accepts_nested_attributes_for :address end class Address < ActiveRecord::Base has_many: persons accepts_nested_attributes_for :persons end Controller: class PersonsController < ApplicationController def new @person = Person.new @person.address = Address.new end def create @person = Person.new params[:person] if @person.save redirect_to :show else render :new end end end class AddressesController < ApplicationController def new @address = Address.new 1.times { @address.persons.build } # increase 1 to default number of people at address end def create @address = Address.new params[:address] if @address.save redirect_to :show else render :new end end Views: # persons/new.html.erb <% form_for @person do |person_fields| %> <% person_fields.fields_for :address do |address_fields| %> <%= address_fields.label ... %> <% end %> <% end %> # addresses/new.html.erb <% form_for @address do |address_fields| %> <% address_fields.fields_for :persons do |person_fields| %> <%= person_fields.label ... %> <% end %> <% end %> If you get weird errors such as HashWithIndifferentAccess, try refreshing your browser, so it pulls in a fresh form. Hope that helps, Mike On Apr 29, 5:18 am, Sereno Mendes <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi all, > > I have a relationship in this form: A person belongs to an address and a > address can have many persons like below: > > class Person < ActiveRecord::Base > belongs_to :address > end > > class Address < ActiveRecord::Base > has_many: persons > end > > The ideia behind this is, if two or more persons share the same address, > isn''t necessary create two indetical rows in the database. But, like i''m > new in the rails development, i want to create a form for person > creation and edition that wraps the creation of related address, as anestedform. I already made a search for a solution for this problem, > but the solutions that i found in others sites are for the inverse > associations, any idea? > > Thanks. > -- > Posted viahttp://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@googlegroups.com. > For more options, visit this group athttp://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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.