0

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 Answer 1

1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.