ruby - Rails - creating a protected default category -
i create special category posts, default , has 3 distinct features:
- it assigned default every created post
- it assigned dependent posts upon destruction of parent category
- it protected deletion anyone
so first point easy implement, not focus on here
the second point more puzzling. guessing should go category model:
before_destroy :move_dependent_posts def move_dependent_posts #no idea goes here end
same problem third point: have check, if category being deleted has id of, say, 1, , after throw error?
before_destroy :protect_default_category def protect_default_category return false if category.id(5) #won't work, of course end
point 2:
before_destroy :move_dependent_posts def move_dependent_posts default_category = category.default_category # find default category here posts.each { |post| post.update_attribute(:category, default_category) } end
point 3:
before_destroy :protect_default_category def protect_default_category throw "default category can't deleted" if id == 1 end
Comments
Post a Comment