0

我有仅适用于子类的回调,但是当我为同级类创建新记录时(两者共享相同的父类)...... child1 的 after_commit 回调正在为 child2 调用,但方法丢失错误。

知道什么会导致这种情况吗?

例如,如果创建了 WarehouseGroup 记录,它会从 WarehouseFile 模型调用 f​​ile_upload_triage 方法。发生这种情况是因为 Parent 类上的 self.inherited 方法吗?如果是这样,您如何为 STI 设置创建单个路由?

家长

module Warehouse
    class WarehouseObject < ApplicationRecord

        include ActiveModel::Dirty

        has_ancestry touch: true
        Gutentag::ActiveRecord.call self

        belongs_to      :user
        belongs_to      :warehouse_group, optional: true
        
        has_many        :shares, :dependent => :destroy, :foreign_key => :warehouse_object_id
        has_many        :link_shares, :dependent => :destroy, :foreign_key => :warehouse_object_id
        has_many        :user_shares, :dependent => :destroy, :foreign_key => :warehouse_object_id
        has_many        :warehouse_metum, dependent: :destroy, foreign_key: :warehouse_object_id

        before_create   :set_parent
        after_create    :update_parent_child_count, if: :has_parent?

        before_update   :clean_custom_thumb

        before_save     :clean_title

        accepts_nested_attributes_for :shares, reject_if: :all_blank, allow_destroy: true

        protected

        def self.inherited(subclass)
            super
            def subclass.model_name
                superclass.model_name
            end
        end

    end
end

第一个孩子

module Warehouse
    class WarehouseGroup < WarehouseObject

        after_update  :check_icon

        after_commit  :create_directory
        after_commit  :check_for_children_ids
        after_commit  :apply_tags_to_children, if: :saved_change_to_tag_names?
        
        after_destroy :remove_directory_and_montage

        after_touch   :refresh_group_data

    end
end

第二个孩子

module Warehouse
    class WarehouseFile < WarehouseObject

        attr_accessor :cropping
        
        before_destroy  :remove_favorite
        after_destroy   :remove_files

        after_commit    :file_upload_triage

    end
end
4

0 回答 0