문제

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 ?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top