gpt4 book ai didi

ruby-on-rails - Rails - 参数数量错误(2 个代表 0..1)错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:10 25 4
gpt4 key购买 nike

我在处理我正在处理的应用程序中的某些代码时遇到问题。
使用以下代码:

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

我收到错误:

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

谁能解释一下发生了什么?

最佳答案

使用查询 API 来保持正确的范围,并且由于 where 是可链接的,所以这样做也更干净:

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

您也可以直接链接它们而无需中间变量:

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

或者您可以将它合并到一个方法调用中:

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

关于ruby-on-rails - Rails - 参数数量错误(2 个代表 0..1)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14776102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com