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 devices with one model, ad_user and device

one ad_user and one device one < code> ad_user .

My models are as follows:

  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]   

I can see the code 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)   

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 device field. device_ids does at a lower level, different way.

Also consider using it, if it fits.

PS Just a small note: In the train you 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

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -