【问题标题】:How to sort some data in view?如何对视图中的一些数据进行排序?
【发布时间】:2011-11-16 13:26:06
【问题描述】:

我正在使用 Ruby 1.8.2 和 Rails 2.3.11。

<% @answered_respondents = Inquiry.find(:all, :conditions => ["question_id = (?) AND is_answered = 1 AND is_denied = 0", q.id]) %>


<% @answered_respondents.each_with_index do |r, i| %>
    <% @nene = Respondent.find(:all, :conditions => ["id = (?)", r.respondent_id ]) %>
      <% @nene.each do |zz| %>
         <span class="statis_answered_resp"><%= zz.email %></span>
      <% end %>
<% end %>

我想做什么:我想通过电子邮件对我的用户(zz.email)进行排序。我该怎么做?

【问题讨论】:

    标签: ruby-on-rails view sql-order-by


    【解决方案1】:

    在你的模型中创建一个默认范围

    Rails2:   default_scope :order => 'email ASC' 
    Rails3:   default_scope order('email ASC') 
    

    默认值就是你所有视图的默认值。

    对于其他排序,您还可以创建其他范围,例如

    Rails2:   named_scope :special :order => 'email ASC' 
    Rails3:   scope :special order('email ASC') 
    

    要使用 therm,你只需说 ModelName.all.special 将您的 ruby​​ 至少升级到 1.8.7 也是一个好主意。

    1.9.2 也很好用,也很棒
    虽然不依赖,但通常与 Rails3 一起使用

    【讨论】:

      猜你喜欢
      • 2014-01-29
      • 2014-07-14
      • 2015-06-25
      • 1970-01-01
      • 2016-03-25
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多