Ruby on rails 如何对视图中的某些数据进行排序?

Ruby on rails 如何对视图中的某些数据进行排序?,ruby-on-rails,view,sql-order-by,Ruby On Rails,View,Sql Order By,我正在使用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 = R

我正在使用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 %>
[“问题id=(?)和被回答=1,被拒绝=0”,q.id])%>
[“id=(?)”,r.responder_id])%>

我想做的是:我想通过电子邮件对我的用户进行排序(zz.email)。如何操作?

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

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一起使用