在使用 "all" 方法时遇到问题并获得 "ArgumentError at"

Having troubles with "all" method and getting "ArgumentError at"

我在这段代码中使用方法 "all" 时遇到问题:

posts = Post.includes(:account).all(:conditions => condition, 
    :order => "created_at DESC", :limit => postsPerPage, 
    :offset => (page-1)*postsPerPage)

当我启动服务器并转到网页时,Padrino 抛出错误:

ArgumentError at / wrong number of arguments (1 for 0)

app/models/post.rb in posts_by_condition

    posts = Post.includes(:account).all(:conditions => condition,
    :order =>"created_at DESC", :limit => postsPerPage, 
    :offset => (page-1)*postsPerPage)

app/models/post.rb in publishedPosts

    posts_by_condition page, postsPerPage, [""]

app/controllers/main.rb in block (2 levels) in <top (required)>

    @posts, @total_pages = Post.publishedPosts(@page, @postsPerPage)

这个问题是我更新padrino包时出现的:

旧版本是 0.10.3 现在是 0.12.5,并且实现了一些其他的 gem:

activerecord 3.1.0 -> 4.2.1

activesupport 3.1.0 -> 4.2.1

haml 3.0.25 -> 4.0.6

以及 padrino 的所有依赖(核心,助手,...)

可能是因为更新的缘故,我试过用其他版本的activerecord可以用,为什么新版本不行?

欢迎任何帮助...谢谢

"all" 方法不接受选项。如果您要查找 return 具有多个条件的记录数组,您可以使用 WHERE。正确的命令是:

posts = Post.includes(:account).where(:conditions => condition, 
    :order => "created_at DESC", :limit => postsPerPage, 
    :offset => (page-1)*postsPerPage)

为清楚起见:

  • .allreturn秒每条记录
  • .where() returns每条记录所在 满足条件
  • .find_by() returns 第一条记录 满足条件
  • .find() returns 第一条记录所在 主键 = 传递的值

每当您看到错误的参数数量 1 for 0 错误时,这意味着您正在尝试将值传递给不接受参数的方法。

至于为什么这个问题才刚刚浮出水面,好像在以前的ActiceRecord版本中有一些优先级允许.all接受参数,但我对这个功能不熟悉,也说不出来详细。 Daiku 对此答案的评论有效地解释了这是如何发生变化的。