简体   繁体   English

Rails - 错误的参数数量(2为0..1)错误

[英]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.

相关问题 Rails:ActionView::Template::Error(参数数量错误(给定 4,预期 0..1)): - Rails: ActionView::Template::Error (wrong number of arguments (given 4, expected 0..1)): Rails - Geocoder - 。参数数量错误(给定3,预期0..1) - Rails - Geocoder - .near wrong number of arguments (given 3, expected 0..1) 升级到 Rails 6 后参数数量错误(给定 4,预期为 0..1) - wrong number of arguments (given 4, expected 0..1) after upgrading to Rails 6 在Rails中导入时,参数数目错误(给定2,应为0..1) - wrong number of arguments (given 2, expected 0..1) while importing in Rails Rails db:种子“rake aborted! ArgumentError:错误的参数数量(2为0..1)“ - Rails db:seed “rake aborted! ArgumentError: wrong number of arguments (2 for 0..1)” ArgumentError(参数数量错误(0..1为3)): - ArgumentError (wrong number of arguments (3 for 0..1)): Geocoder:ArgumentError:参数数目错误(0..1为3) - Geocoder : ArgumentError: wrong number of arguments (3 for 0..1) ArgumentError(arguments 的编号错误(给定 2,预期 0..1) - ArgumentError (wrong number of arguments (given 2, expected 0..1) redirect_to的参数数目错误(0..1为2) - wrong number of arguments (2 for 0..1) at redirect_to 当我包含“Rails.application.routes.url_helpers”时,参数数量错误(0为0..1) - wrong number of arguments (3 for 0..1) when i include “Rails.application.routes.url_helpers”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM