Search code examples
ruby-on-railsrubyactiverecordpadrino

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


I'm having troubles using the method "all" in this code:

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

When i start the server and go to the web page Padrino throws an error:

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)

This problem just appeared when i updated the bundle of padrino:

The old version was 0.10.3 now is 0.12.5, and with that some other gems were actualized:

activerecord 3.1.0 -> 4.2.1

activesupport 3.1.0 -> 4.2.1

haml 3.0.25 -> 4.0.6

And all dependence of padrino (core, helper, ...)

Probably this is because of the update, i tried use the other version of activerecord and it works, why not in the new version?

Any help will be welcome... Thanks


Solution

  • The "all" method doesn't take options. If you're looking to return an array of records with a number of conditions you want to use WHERE. The correct command would be:

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

    For clarity:

    • .all returns every record
    • .where() returns every record where the conditions are met
    • .find_by() returns the first record where the conditions are met
    • .find() returns the first record where the primary key = the passed value

    Whenever you see a wrong number of arguments 1 for 0 error, it means you're trying to pass values to a method which does not accept parameters.

    As for why this problem only just surfaced, it appears there was some precedence for allowing .all to accept parameters in previous versions of ActiceRecord, but I am not familiar with this functionality, and cannot speak to it in detail. Daiku's comment on this answer effectively explains how this has changed.