如何重写:为了在的has_many定义 [英] How to override :order defined in a has_many

查看:82
本文介绍了如何重写:为了在的has_many定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类的著者
的has_many:书:为了=> 名ASC
 

我想查询所有的书籍按名称排序降序

  Authors.books.order(名DESC)
 

但结果是

  SELECT * FROM .... ORDER BY名ASC,名降序
 

和结果回来的名排序ASC

有没有办法删除原来的秩序,协会或覆盖它?或者是指定关系中的一个订单一个坏主意?

使用Rails 3.0.3

解决方案

  Authors.books.reorder(名DESC)
 

I have

class Authors 
has_many :books, :order => 'name ASC'

I am trying to query all the books sorted by name DESC

Authors.books.order('name DESC')

but the result is

SELECT * FROM .... ORDER BY name ASC, name DESC

and the results come back with the name sorted ASC

is there a way to remove the original order in the association or override it? Or is specifying an order in a relation a bad idea?

using Rails 3.0.3

解决方案

Authors.books.reorder('name DESC')

这篇关于如何重写:为了在的has_many定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆