开发者

Ignore validation failure when using accepts_nested_attributes_for (i.e. prevent rollback)?

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 开发者_JS百科the database (at which time I want to reject the ones that remain invalid)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜