ruby - Rails 3.2.1 associations no callbacks? -
First of all, I am using Rail 3.2.1 and ruby 1.9.3p392
I have many one My models are as follows: I can see the code Any of the callbacks are called and in spite of the fact that zero attendance should be verified, despite the fact that < Code> ad_user_id has expired Am I doing anything wrong here / Unfortunately, I think this is a required behavior when you update associations, you do not trust the callback. Want Therefore, the underlying model is contained as may be updated from What you can do, however, instead attempted to reach Also consider using it, if it fits. PS Just a small note: In the train you devices with one model, ad_user and device
ad_user and one
device one < code> ad_user .
Class AdUser & lt; ActiveRecord :: Base has has_many: Tools class tool & lt; ActiveRecord :: Base belongs_to: DEVICE_TYPE belongs_to: device_status belongs_to: ad_user validates_presence_of: Name validates_uniqueness_of: Name validates_presence_of: serial validates_uniqueness_of: serial validates_presence_of: device_type_id validates_presence_of: device_status_id validates_presence_of: ad_user_id before_update: before_update_call before_save: before_save_call before_create: before_create_call before_validation: before_validation_call def protect before_update_call P Self.name p self.ad_user_id p "- = - = - = - = - = - = - = - = -" p "before_update_call" self.ad_user_id = 1 if self.ad_user_id.nil? || Self.ad_user_id.blank? Def end before_save_call p self.name p self.ad_user_id p "- = - = - = - = - = - = - = - = -" "before_save_call" self.ad_user_id = 1 If self.ad_user_id.nil P? || self.ad_user_id.blank? Def end before_create_call p self.name p self.ad_user_id p "- = - = - = - = - = - = - = - = -" "before_create_call" self.ad_user_id = 1 If self.ad_user_id.nil p? || self.ad_user_id.blank? Def end before_validation_call p self.name p self.ad_user_id p "- = - = - = - = - = - = - = - = -" "before_validation_call" self.ad_user_id = 1 If self.ad_user_id.nil P? || self.ad_user_id.blank?
u = AdUser.first u.device_ids = [1,2]
before_validation_call ,
before_save_call and
before_update_call print for the console, but when I unassign these devices from the user with:
u.device_ids The result of this is a simple:
Set up SQL (2.0ms) update "Tools" "ad_user_id" = Where Where "Devices" . "Ad_user_id" = 405 and "Tools". In the "ID" (332, 333)
device field.
device_ids does at a lower level, different way.
self.ad_user.present? can use
self.ad_user_id.nil? || self.ad_user_id.blank? . Additionally, you can merge the
validates_presence_of statement.
Comments
Post a Comment