views:

15

answers:

1

So suppose I have the Person and Child models:

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

class Child < ActiveRecord::Base
    belongs_to :parent, :class_name => "Person"
    validates_presence_of :name
end

Now when I use a nested form and save a Person with 2 new children, the entire transaction will fail if one of the children fails to validate (i.e. it will rollback).

How do I ignore this validation failure and just save the 1 Person and 1 child that are valid? I do not want the entire transaction to fail because 1 child failed the validation. I simply want to save the valid records...

Help much appreciated, thanks!

P.S. using :reject_if if not an option for me because I need to be able to access the invalid records up until the moment I save it into the database (at which time I want to reject the ones that remain invalid)

A: 

You could solve it without "accepts_nested_attributes_for :children" and save the single objects in your controller seperately...

Lichtamberg