B04a553de1b8b45433b841bba440cc42

Please try refactor this small piece finding all subcategories, including its parent.

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 !

A8d3f35baafdaea851914b17dae9e1fc

Adam

September 24, 2009, September 24, 2009 16:32, permalink

No rating. Login to rate!

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
B04a553de1b8b45433b841bba440cc42

papricek.myopenid.com

September 24, 2009, September 24, 2009 17:54, permalink

No rating. Login to rate!

Thanks! ;)

Your refactoring





Format Copy from initial code

or Cancel