检查是否有依赖 before_destroy rails admin ruby

Check if there's dependency before_destroy rails admin ruby

我目前正在做一个项目,在我们的项目中有 2 个 table/classes ActivityType 和 Activity。同样在这个项目中,我们使用了一个名为 rails 的 gem 管理员。如果在 activity.

中使用了 activity 类型,则管理员无法删除 activity 类型

Activity

class Activity < ApplicationRecord
  belongs_to :activity_type
end

Activity类型

class ActivityType < ApplicationRecord
  has_many :activities
before_destroy :ensure_has_no_activity_type

  private

  def ensure_has_no_activity_type
      unless activities.count == 0
       errors[:base] << "cannot delete activity type that has activity"
       return false
    end
  end
end

问:如何查看activity中是否有child?我的代码有问题吗?

这就是我的意思

def ensure_has_no_activity_type
  if activities.present?
    errors.add(:base, 'Cannot delete activity type that has activity')
    throw(:abort)
  end
end