@users = User.all
<% @users.each do |user| %>
<%= user.name %>
<% end %>
How would I sort this by name and not the id
@users = User.all
<% @users.each do |user| %>
<%= user.name %>
<% end %>
How would I sort this by name and not the id
@users = User.order('name ASC')
在你的控制器中
如建议的那样,您还可以设置而不是到处default_scope
调用:User.order('name asc')
default_scope order('name ASC')
或者您可以设置范围:
scope :name_ordered, order('name ASC')
并调用它
User.name_ordered
除了 Marek Lipka 回答:可能是您希望每次都获得订单的用户,然后添加到模型中
default_scope order('name asc')
而不是把它放在任何地方
User.order('name asc').all