1

I have this statement in my application

User.all(:conditions => "status <> 3", :order => "firstname, lastname").collect{|x| [x.name, x.id.to_s]}

where I am trying to find all the users and sort them by first name and last name.

When I try using this statement it gives me the error

wrong number of arguments (given 1, expected 0).

Can anyone please help me with this?

5
  • What function is the error being caused by? Commented Feb 28, 2017 at 17:52
  • code def user_options options = User.all(:conditions => "status <> 3", :order => "firstname, lastname").collect{|x| [x.name, x.id.to_s]} end Commented Feb 28, 2017 at 17:55
  • No, what function in the snippet above has the wrong number of arguments supplied? The error should say. Commented Feb 28, 2017 at 17:57
  • The error shows this line options = User.all(:conditions => "status <> 3", :order => "firstname, lastname").collect{|x| [x.name, x.id.to_s]} Commented Feb 28, 2017 at 17:59
  • I know the query needs to be changed for rails 4 by not sure how to change it.. Commented Feb 28, 2017 at 18:02

1 Answer 1

2

Another way to write this

User.where('status <> 3').order(:firstname, :lastname).pluck(:name, :id)

The only difference is that the id won't be converted to a string so you can just append .map { |res| res[1] = res[1].to_s; res } if you really need this.

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

2 Comments

using the query as options = User.where('status <> 3').order(:firstname, :lastname).collect{|x| [x.name, x.id.to_s]}worked...
Just a note here, pluck can potentially perform better. If you use collect it means that you instantiate all ActiveRecord objects, while pluck will return a more "lightweight" array. I think the documentation puts it nicely. "Use pluck as a shortcut to select one or more attributes without loading a bunch of records just to grab the attributes you want."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.