2016-12-26 63 views
0

我在我的控制器中有一个简单的包含语句,但包含表的顺序是默认ID。 在我的情况下,我需要以与接收uuid相同的方式排列元素。Rails包括与订购不ID

查询:

compare_uuids = {xyz, ytb, tyd} 
@books = Book.where(uuid: compare_uuids).includes(:venue, :related_publisher, :related_author) 

上述表达式反应良好,但说,如果他们的ID的UUID订货如:

id  uuid 
5  xyz 
1  ytb 
2  tyd 

组成表达式时的发言来看是为了通过ID。所以uuid订单就会丢失。 有没有解决它的方法。

回答

0

就试试这个...

has_many :books, :order => 'priority DESC' 

在轨道4,这是现在已经过时,新的办法将是

has_many :books, -> { order(:priority => :desc) } 

获取输出:

你可以尝试

Authors.books.except(:order).order('priority DESC') 
# OR 
Authors.books.reorder('priority DESC') 
+0

我在Book.r中添加了上面这行代码B模型...但它不工作 –

+0

你现在可以检查.. –

+0

@SyedAsadAbbasZaidi你检查吗? –