marked for destruction

Today I had gone through an issue, which got fixed by using the marked_for_destruction? method. This marked_for_destruction is part of Active Record Autosave Association module. Autosave Association module takes care of automatically saving associated records when their parent is saved. For more details see Auto Save Association .

In my project I have a model called Directory and it has many Categories. But I have to add a validation that there should be atleast one category associated with the directory. So in the directory model I added a validation in the after_save callback, which will check the count of Categories is greater that 0.

But while we do the update directory if I deleted all the categories the validation won't work , because I am using Autosave Association, which  saves category only after directory got saved. So at the time of Directory got saved the category count was greater than one, so validation will pass. To fix this issue I changed the condition that the categories count should be greater than 1. This is handled in the Directory model, and I want to let the controller know about the Directory not get saved. For that I will throw an exception if the condition fails.

after_save: check_categories_count

def  check_categories_count
   if self.categories.count <= 1
      throw Exception
   end
end

Since the condition is checking for categories greater than 1 this will fail for update with only one category. Because on update also it will execute the after_save and there is only 1 category associated with this directory. But it should fail if we are deleting the only one category. So I added one more condition that checks if the only one category is marked for delete. So the condition becomes:

if self.categories.first.nil? || self.categories.first.marked_for_destruction?
   throw Exception
end

Now it will fail only if the user tries to delete the last category.

blog comments powered by Disqus
Devise with token based aut... →
← Write a named scope to fetc...