I am currently doing a project and in our project there's 2 table/classes ActivityType and Activity. Also in this project, we've use a gem called rails admin. The administrator can't delete a activity type if there's an activity type used in the activity.
Activity
class Activity < ApplicationRecord
belongs_to :activity_type
end
Activity Type
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
Question: How can I check if there's a child in activity? Is there something wrong in my code?