2011-11-08 28 views
1

出於某種原因,它似乎像.each塊迭代只有一次,當我寫可以說,3個或4個評論,@total_comments只顯示1,而不是3或4紅寶石。每塊不加起來的所有對象

的關係是:

  • 用戶有很多帖子
  • 後有很多評論

任何人可以幫助嗎?

控制器

@total_comments = 0 
@posts = current_user.posts 

@posts.each do |post| 
@comments = post.comments 
@new_comments = @comments.count [email protected]("created_at < ?",1.day.ago).count 
@total_comments += @new_comments 
end 

視圖

<%= @total_comments %> 
+0

肯定比它需要更復雜? – 2011-11-08 19:33:35

+0

只需在SQL中使用COUNT(*),JOIN和GROUP BY語句,或者使用等效的Rails助手,就可以獲得更簡單快捷的版本。 – robbrit

+0

這裏的問題是,如上所述,你可以做到這一點更簡單。這是n + 1的經典例子(如果有10個帖子,這將導致對您的數據庫有11個查詢)。嘗試使用上述建議進行清理。另外,您並不需要製作@評論,@ old_comments和@ new_comments實例變量。 – mynameiscoffey

回答

2

要獲得的所有評論數,您希望是這樣的

@total_comments = Comment.count(
    :conditions => ["post.user_id = ? and comment.created_at >= ?", 
     current_user.id, 1.day.ago], 
    :include => :post 
) 

雖然不知道你的數據模型是很難說清楚。

與@m_x,一整潔和多個軌道-3/1.9紅寶石版本式的上面討論

@total_comments = Comment 
       .joins(post: :user) 
       .where(users: {id: current_user.id}) 
       .where("created_at < ?", 1.day.ago) 
       .count 
+1

爲什麼不使用'@ post.user.comments'? –

+0

@RyanBigg我們不知道OP的數據模型 - 有很多*可能性 - 我的答案的重點是表明您可以使用AR/SQL聚合函數而不是n + 1查詢。 – Emma

+0

@Ryan:你說得對,簡單得多。 –

0

看起來你忘了你保存的意見 - 有些意見不保存到數據庫,這就是爲什麼你也失去了一些意見。

此外,您可以使用自動保存選項,每當您更改它們時自動保存您的變量。

1

使用這個代替:

@total_comments = Comment 
        .joins(post: :user) 
        .where(users: {id: current_user.id}) 
        .where("comments.created_at < ?", 1.day.ago) 
        .count 

EDIT

我絕對需要一些睡眠。作爲@Ryan指出,這是更簡單:

@total_comments = current_user.comments.where("created_at < ?", 1.day.ago).count 

...但OP已經把它添加到他的User型號:

has_many :comments, through: :posts 
+0

首先想到@Emama的回答比較好,因爲我認爲'count'是某種'Array'方法。 [但我檢查](http://guides.rubyonrails.org/active_record_querying。html#計算)ans' count'調用關係_does_生成SQL COUNT()'查詢。我今晚學到了一些東西。 –

+0

你說得對!我試圖得到的是使用一組參數來結束一個單一的SQL查詢,而不是一個n + 1的查詢很難遵循邏輯。鞦韆和環島。 – Emma

+1

的確,我仍然認爲使用'joins'會比使用'includes'更合適。畢竟,沒有必要急於加載帖子。如果您同意,請隨時編輯您的帖子。 –