After_add callback for has_one

This seems to be an inconsistency between has_many and has_one.

The has_many association allows you to specify the after_add callback that is called after an object is added to the collection.

class Person has_many :parents, :after_add => { puts "Added new parent" } # allowed has_one :car, :after_add => { puts "Added car" } # not allowed end class Car after_create :assign_name def assign_name self.name = "Herbie" end end 

Unfortunately, there is no after_add callback for the has_one association. How do you do the same for has_one?

+4
source share
1 answer

I think you can use before_save and check if the attitude towards the machine has changed:

 before_save :do_something def do_something puts "Added car" if car_changed? end 
+1
source

All Articles