In addition to mapping out the Mandelbrot set, ruby’s inject method can also be used to easily find and/or create nested categories given a breadcrumb path.
Assuming Category is a Ruby on Rails ActiveRecord class that acts_as_tree, and breadcrumbs is an array of breadcrumb strings, e.g. breadcrumbs = [’Widgets’, ‘Green Widgets’, ‘14V Widgets’] :
The final “id && Category.find(id)” is to have the function return nil in case breadcrumbs is empty.
This returns the final Category object if it exists, and creates any or all components of the path as needed.
No Comments, Comment or Ping
Reply to “ruby inject and category breadcrumbs”