This project is archived and is in readonly mode.

#2815 ✓resolved
sarah (at ultrasaurus)

nested models: build should directly assign the parent

Reported by sarah (at ultrasaurus) | June 18th, 2009 @ 09:55 PM | in 2.3.6

As noted in https://rails.lighthouseapp.com/projects/8994/tickets/1943 sometimes you want to validate the presence of the parent from the child (to handle the case where the child could be independently created for an existing parent)

Example:

class Person < ActiveRecord::Base
has_many :children accepts_nested_attributes_for :children end

class Child < ActiveRecord::Base
belongs_to :person

#Validating that a child has a parent validates_presence_of :person # as opposed to person_id, if the person is a new_record? w/o id yet end

p = Person.new(:name => "Smith", :children_attributes => [{:name => "John"}] )
p.valid? #=> false (should be true)
p.save #false (should be true)
p.children.first.errors.on(:person_id) #=> "can't be blank"

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages