ruby - Rails - creating a protected default category -


i create special category posts, default , has 3 distinct features:

  1. it assigned default every created post
  2. it assigned dependent posts upon destruction of parent category
  3. 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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -