def self.find_with_children_by_slug(slug)
category = Category.find_by_slug(slug)
category.direct_children << category
rescue ActiveRecord::RecordNotFound
[]
end
Refactorings
No refactoring yet !
Adam
September 24, 2009, September 24, 2009 16:32, permalink
I think what you have is fine, except last time I checked, find_by_* doesn't raise RecordNotFound.
class Category < ActiveRecord::Base
self.find_with_children_by_slug(slug)
category = find_by_slug(slug, :include => :direct_children)
category ? category.direct_children << category : []
end
end
Please try refactor this small piece finding all subcategories, including its parent.