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 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)


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 = => "Smith", :children_attributes => [{:name => "John"}] )
p.valid? #=> false (should be true) #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=""></a>

Referenced by