Ruby-on-rails 按升序排序 Rails
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16456341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort in Ascending Order Rails
提问by muhihsan
Hi I have this model
嗨,我有这个模型
Model item
模型项目
class Inventory::Item < ActiveRecord::Base
has_many :types, :class_name => "ItemType"
attr_accessible :name
end
Model item_type
模型 item_type
class Inventory::ItemType < ActiveRecord::Base
belongs_to :item
attr_accessible :number
end
then let say in controller I want to sort types (which has class ItemType) in ascending order based on Item name. How do I do that?
然后在控制器中说我想根据项目名称按升序对类型(具有类 ItemType)进行排序。我怎么做?
For example,
例如,
- ItemType number = 1 has Item name = Table
- ItemType number = 2 has Item name = Chair
- ItemType number = 3 has Item name = Window
- ItemType number = 4 has Item name = Computer
- ItemType number = 1 有 Item name = Table
- ItemType number = 2 has Item name = Chair
- ItemType number = 3 有 Item name = Window
- ItemType number = 4 有 Item name = Computer
So instead of sorting it from number, I want it sorted based on item.name(ASC) like this:
因此,我希望它根据 item.name(ASC) 进行排序,而不是从数字中排序,如下所示:
- ItemType number = 2 has Item name = Chair
- ItemType number = 4 has Item name = Computer
- ItemType number = 1 has Item name = Table
- ItemType number = 3 has Item name = Window
- ItemType number = 2 has Item name = Chair
- ItemType number = 4 有 Item name = Computer
- ItemType number = 1 有 Item name = Table
- ItemType number = 3 有 Item name = Window
回答by Brad Werth
Something like this should do the trick...
这样的事情应该可以解决问题......
ItemType.includes( :item ).order( 'inventory_items.name DESC' )
Also, if you need to do this in many locations, you can accomplish the same thing by providing an :orderparameter to your has_manycall, instead - http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many.
此外,如果您需要在多个位置执行此操作,您可以通过:order为您的has_many调用提供一个参数来完成相同的操作,而不是 - http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many。
回答by Andy Hayden
To retrieve records from the database in a specific order, you can use the ordermethod:
要以特定顺序从数据库中检索记录,您可以使用以下order方法:
Item.order(:name)
by default this sorts ascending.
默认情况下,这是升序排序。
回答by Arif
You can also set default order in your Model like this:
您还可以在模型中设置默认顺序,如下所示:
default_scope order("#{self.table_name}.item_name ASC")
This will sort items by item_name without any change in controller
这将按 item_name 对项目进行排序,而不会更改控制器
回答by Jyothu
For making ASC (Default sorting mode) for name kind of fields (Alphabets),
为名称类型的字段(字母表)制作 ASC(默认排序模式),
You can use ORDER BY Clause in MySQL
Hence, In Rails you can simply use
因此,在 Rails 中,您可以简单地使用
Model.order(:field_name)
回答by Lian
In your query, you can use/add ORDER BY itemType ASC
在您的查询中,您可以使用/添加 ORDER BY itemType ASC

