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]+'%'])
@herbi=@herbivores.find(:all,:conditions =>['city like?', '%bad%'])
I receive the error:
我收到错误:
wrong number of arguments (2 for 0..1)
Can anybody explain what is happening?
谁能解释一下发生了什么?
2 个解决方案
#1
3
Use the query API to keep the correct scope, and also do this more cleanly since where
is chainable:
使用查询API来保持正确的范围,并且还可以更干净地执行此操作,因为可链接的位置:
@herbivores=Deer.where('state like ?', '%' + params[:number]+'%')
@herbi=@herbivores.where('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%")
#2
0
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.
我相信正在发生的事情是你正在将@herbivores视为你可以找到的模型,但它是一个Deer记录数组,因此它不是模型。
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
#1
3
Use the query API to keep the correct scope, and also do this more cleanly since where
is chainable:
使用查询API来保持正确的范围,并且还可以更干净地执行此操作,因为可链接的位置:
@herbivores=Deer.where('state like ?', '%' + params[:number]+'%')
@herbi=@herbivores.where('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%")
#2
0
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.
我相信正在发生的事情是你正在将@herbivores视为你可以找到的模型,但它是一个Deer记录数组,因此它不是模型。
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