Limit “each” list in Rails

前端 未结 3 1546
借酒劲吻你
借酒劲吻你 2021-02-05 11:42

We have this:

<% @shops.each do |shop| %>
  
  • <%= shop.name %>
  • <% end %>

    The code will yield the total res

    3条回答
    •  遇见更好的自我
      2021-02-05 12:00

      Just limit it on your ActiveRecord level (or SQL)

      @shops = Shop.limit(20)        # Rails 3+
      @shops = Shop.all :limit => 10 # Rails 2+
      

      Or use Ruby

      <% @shops[0,20].each do |shop| %>
        
    • <%= shop.name %>
    • <% end %>

    提交回复
    热议问题