10

I'm trying to use sort on each do. I get the error

wrong number of arguments(1 for 0)

I understand that I cannot daisy chain them together. Does anyone know another methods of getting this done.

  <% Category.sort(:id).limit(4).each do |type| %>
      <%= type.name %>
  <% end %>

The result I am aiming for is to have all categories listed from a to z.

4
  • try { instead of do, and }, instead of end Commented Jan 29, 2014 at 5:07
  • 6
    If Category is an ActiveRecord model, you want .order(:id).limit(4). Commented Jan 29, 2014 at 5:07
  • Is Category an array or AR ? Commented Jan 29, 2014 at 5:18
  • @meagar, Thanks alot. I actually did try .order but I used id instead of :id. Thanks. Commented Jan 29, 2014 at 5:21

1 Answer 1

14

Assuming the Category is an Active Record then

<% Category.order(:id).limit(4).each do |type| %> would do the trick.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks. But if you look under the comments, you will notice you provide the same answer as meagar
oh,I didn't notice that.
@Pavan What does the .limit(4) do here? And how do I do the sorting desceding?
@TasosAnesiadis It's been a while but maybe this will help someone else. .limit(4) shows only 4 last records from the query result. You can use .order("created_at desc"). Replace created at with whatever you want to take into account for sorting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.