Rails 3.0.10 before_validation callback not called for related collection objects

I have an object called a parent that has_many child objects:

has_many :children accepts_nested_attributes_for :children, :allow_destroy => true 

The child includes a module that indicates a callback :before_validation :

 def self.included base base.class_eval do before_validation :my_callback end end protected def my_callback logger.debug "see me!" end 

I noticed that when creating parent and nesting attributes for children, the callback :before_validation does not start for each Child. Is this the intended behavior? I tried making a before_save instead, and it works fine.

This is on Rails 3.0.10.

Thanks!

+4
source share
1 answer

You should use validates_associated :

 class Parent < ActiveRecord::Base has_many :children accepts_nested_attributes_for :children, :allow_destroy => true validates_associated :children end 
+7
source

Source: https://habr.com/ru/post/1381203/


All Articles