views:

317

answers:

1

I have a select input:

f.select :category_id, nested_set_options(Category, @categories) {|i| "#{'-' * i.level} #{i.name}" }

What is the most efficient way to show only the categories with level > 1 ?

+1  A: 

If your categories array is already being retrieved from the database (i.e. this is not the only call to retrieve the categories on the page) and you do not anticipate the array holding hundreds of categories, you can do:

@categories.to_ary.find { |cat| cat.level > 1 }

This does a find on the array, rather than through the database. Your other option would be to use a named_scope.

Homar
The named_scope is a good idea, thanks
astropanic