我有一个像这样的控制器方法:
def index
@categories = Category.all
end
如何按字母顺序排列@categories
个名字?
答案 0 :(得分:6)
您可以order
:
@categories = Category.order(:name)
答案 1 :(得分:1)
在你的类别控制器中:
@Getter @Setter private String foo;
默认情况下,这会按字母顺序排列 class CategoriesController < ApplicationController
def index
@categories = Category.order(:name)
end
end
列。
答案 2 :(得分:0)
#default ascending
class CategoriesController < ApplicationController
def index
@categories = Category.order(:name)
end
end
#explicit ascending if you were chaining a scope that has some other default order
class CategoriesController < ApplicationController
def index
@categories = Category.tags.order(name: :asc)
end
end
#decending
class CategoriesController < ApplicationController
def index
@categories = Category.tags.order(name: :desc)
end
end