[英]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]+'%'])
@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? 谁能解释一下发生了什么?
Use the query API to keep the correct scope, and also do this more cleanly since where
is chainable: 使用查询API来保持正确的范围,并且还可以更干净地执行此操作,因为
where
是可链接的:
@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%")
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.