2015-06-01 15 views
1

我在此代碼使用方法「全部」有麻煩:經與「所有」的方法和獲得的煩惱「引發ArgumentError在」

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,並與其他一些寶石被現實化:

了activerecord 3.1.0 - > 4.2.1

的ActiveSupport 3.1.0 - > 4.2.1

HAML 3.0.25 - > 4.0.6

而且padrino的所有依賴性(芯,幫手,...)

可能這是因爲更新,我試圖使用其他版本的activerecord,它的工作原理,爲什麼不在新版本?

任何幫助將受到歡迎...謝謝

回答

1

「全」的方法並不需要的選項。如果你想要返回一系列具有多個條件的記錄,你想使用WHERE。正確的命令是:

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

爲清楚:

  • .all回報每一條記錄
  • .where()回報每一個地方的 條件得到滿足
  • .find_by()返回的第一條記錄,其中記錄 條件得到滿足
  • .find()返回其中 主鍵=傳遞的值

每當你看到一個錯誤的參數數目1 0錯誤,這意味着你要值傳遞到不一個方法的第一個記錄接受參數。

至於爲什麼這個問題纔剛剛浮出水面,似乎有一些優先允許。所有接受ActiceRecord 的以前版本的參數,但我不熟悉這個功能,並且無法詳細它說話。 Daiku對這個答案的評論有效地解釋了這個變化。

+2

康納的答案是正確的。它突然中斷的原因是'all'方法在active_record中改變了4.現在它的行爲與舊的'scoped'類似,不再立即運行查詢。 – Daiku

+0

這很有用,非常感謝你們兩位。 –