本文介绍了Ruby 错误:参数数量错误(0 代表 1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组名为@microposts 的微博

I have an array of microposts named @microposts

原来如此

@microposts=Micropost.all
@[email protected](:kind => "purchase")

然后它给了我错误

ArgumentError: wrong number of arguments(1 for 0)
    from (irb):23:in `select'
    from (irb):23

我不确定 select 是否是我需要的.我正在尝试哪里,但这也不起作用.我也试过 @[email protected](kind:"sale") 以防万一,但这也不起作用.

I am not sure if select is what I need. I was trying where but that didn't work either. I also tried @[email protected](kind:"sale") just in case but that didn't work either.

如果不清楚,kind 是 microposts 表中的一列,表示销售"或购买".我希望@purchases 成为购买"类型的微博数组

If its not clear, kind is a column in the microposts table that indicates either "sale" or "purchase". I want @purchases to be the array of microposts that is the "purchase" kind

推荐答案

您好,您使用 select 的方式有误.您应该先使用 select像这样试试

Hi you are using select in wrong way. You should use select before alltry it like this

@purchases = Micropost.where(:kind => "purchase")

这将对数据库进行另一个查询.如果你不想要这个并尝试使用@microposts 列表对象,你可以这样做

this will do another query to db. If you don't want this and try using the @microposts list object, you can do this

@microposts=Micropost.all
@[email protected]{ |m| m if m.kind == "purchase"}.compact

我认为 kind 是 micropost 对象的属性.

where I suppose kind is the attribute of micropost object.

谢谢

这篇关于Ruby 错误:参数数量错误(0 代表 1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:58