简体   繁体   English

如何在RoR上订购检索到的活动记录

[英]How to order retrieved Active Records on RoR

I am trying to retrieve ordered records from the ddbb and show them on a list. 我正在尝试从ddbb检索有序记录并将它们显示在列表上。 I get the right records but they are not ordered. 我得到正确的记录,但没有排序。

There is a column on rewards named "credits" 关于奖励的专栏名为“信用”

@rewards = Reward.where(:merchant_id => @merchant.id, :order => :credits)


<ul class="list-group">
    <% @rewards.each do |reward| %>
        <li class="list-group-item"><%= reward.name %>
            <span class="badge"><%= reward.credits %></span>
        </li>
    <% end %>
</ul>

I have also tried with no success: 我也尝试过但没有成功:

 @rewards = Reward.where(:merchant_id => @merchant.id).order(credits: :desc)

I get something like this: 我得到这样的东西:

NAME Credits name1 - 20 name2 - 4 name3 - 25 NAME积分name1-20 name2-4 name3-25

 @rewards = Reward.where(:merchant_id => @merchant.id).order('credits desc')

您在此处混合了rails 4语法,因为您正在使用rails 3而无法使用

We have the following in our code, and it works, different syntax for rails 4 我们的代码中包含以下内容,并且可以正常运行,Rails 4的语法不同

In Rails 3.2.11, Mongoid 在Rails 3.2.11中,Mongoid

Account.order_by(created_at: :desc)
or 
Account.order_by('created_at desc')

In Rails 4.0.3, Postgres + Activerecord 在Rails 4.0.3,Postgres + Activerecord中

Account.order(created_at: :desc)
also this will work
Account.order('created_at desc')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM