我想弄清楚如何在保存回調後告訴數組中有什麼變化。下面是我使用的代碼示例:確定在Rails after_save回調中從數組中刪除了哪些id?
class User < ActiveRecord::Base
has_many :user_maps, :dependent => :destroy
has_many :subusers, :through => :user_maps, :dependent => :destroy
has_many :inverse_user_maps, :class_name => "UserMap", :foreign_key => "subuser_id"
has_one :parent, :through => :inverse_user_maps, :source => :user
after_save :remove_subusers
def remove_subusers
if self.subuser_ids_were != self.subuser_ids
leftover = self.subuser_ids_were - self.subuser_ids
leftover.each do |subuser|
subuser.destroy
end
end
end
end
class UserMap < ActiveRecord::Base
belongs_to :user
belongs_to :subuser, :class_name => "User"
end
我與after_save的回調去除subusers因爲我無法獲得相關的破壞功能通過user_maps工作。有沒有人有辦法做到這一點?
謝謝!
顯示您的子用戶模型 –
沒有子用戶模型。這是一種自我參照關係。 – NineBlindEyes