Rails 4 - 订购范围

时间:2016-01-09 07:02:37

标签: ruby-on-rails scopes

我正试图在Rails 4中创建一个应用程序。

我的资格模型中有两个范围:

scope :completed, ->{ where(pending: !true) }
scope :pending, -> { where(pending: true) }

我想在我的视图中列出它们(最新的第一个)。

我有这个视图文件:

<% Qualification.pending.sort_by.year_earned.asc.each do |qualification| %>
  <div class="row">
    <div class="col-md-12">
      <div class="profilequalifications">
        <%= qualification.current_study  %>
      </div>    
    </div>  
  </div>
<% end %>

<% Qualification.completed.sort_by(&:year_earned).each do |qualification| %>
  <div class="row">
    <div class="col-md-12">
      <div class="profilequalifications">
        <%= qualification.completed_award  %>
      </div>    
    </div>  
  </div>
<% end %>   

第二个索引有效 - 但顺序错误。

第一个索引 - 我在表达式上尝试了一百万个变体,但找不到一个不会抛出错误的变体。

我已经尝试了以上各项以及以下各项(以下是我在本网站上发现的示例):

<% Qualification.pending.sort_by(&:year_earned).reverse_order.each do |qualification| %>

<% Qualification.pending.sort_by(&:year_earned.reverse).each do |qualification| %>

<% Qualification.pending.sort_by('&:year_earned ASC').each do |qualification| %>


<% Qualification.pending.sort_by('year_earned ASC').each do |qualification| %>

而不是全部列出 - 有没有人知道如何按升序列出?

2 个答案:

答案 0 :(得分:0)

我想你想要byte[] globalBuffer = new byte[4000]; //large buffer, put globally //In your data received, use Buffer.BlockCopy to copy data to your globalBuffer //Beware the index if (globalBuffer.Length >= 14){ //less than this length, then the data is incomplete //Do the checking if length is at least 14 }

有关详情,请参阅此处:http://apidock.com/rails/ActiveRecord/QueryMethods/order

答案 1 :(得分:0)

我会将订单包含在您的范围内。

scope :completed, ->{ where(pending: !true).order('year_earned DESC') }
scope :pending, -> { where(pending: true).order('year_earned DESC')}

在您的视图中,只需删除.sort_by,然后就可以了。

<% Qualification.pending.each do |qualification| %>
 <div class="row">
  <div class="col-md-12">
    <div class="profilequalifications">
     <%= qualification.current_study  %>
    </div>    
   </div>  
  </div>
<% end %>

<% Qualification.completed.each do |qualification| %>
 <div class="row">
  <div class="col-md-12">
   <div class="profilequalifications">
    <%= qualification.completed_award  %>
   </div>    
  </div>  
 </div>
<% end %>