user2054628
user2054628

Reputation:

Rails - wrong number of arguments (2 for 0..1) error

I am having trouble with some code inside an application I am working on.
With the following code:

@herbivores=Deer.find(:all,:conditions =>['state like?', '%' + params[:number]+'%'])
@[email protected](:all,:conditions =>['city like?', '%bad%'])

I receive the error:

wrong number of arguments (2 for 0..1)

Can anybody explain what is happening?

Upvotes: 1

Views: 2209

Answers (2)

PinnyM
PinnyM

Reputation: 35531

Use the query API to keep the correct scope, and also do this more cleanly since where is chainable:

@herbivores=Deer.where('state like ?', '%' + params[:number]+'%')
@[email protected]('city like ?', '%bad%')

You can also chain these directly without an intermediate variable:

@herbi = Deer.where('state like ?', "%#{params[:number]}%").where('city like ?', "%bad%")

Or you can merge it into one method call:

@herbi = Deer.where('state like ? AND city like ?', "%#{params[:number]}%", "%bad%")

Upvotes: 3

Craig Taub
Craig Taub

Reputation: 4179

I believe what is happening is that you are treating @herbivores like its a model that you can find on, but it is an Array of Deer records so is not a model.

EDIT: Purhaps you want:

@herbivores=Deer.find(:all,:conditions =>['state like ?', "%#{params[:number]}%"])
@herbivores.each do |herbi|
  if herbi.city == 'bad'
    puts "bad city in state #{ani.state}"
  end
end

Upvotes: 0

Related Questions