2011-10-27 27 views
0

我有2種型號在我的Rails 3應用程序得到遵循用戶的職位與acts_as_follower

用戶:

class User < ActiveRecord::Base 

    acts_as_followable 
    acts_as_follower 

    has_many :posts 

end 

帖子:

class Post < ActiveRecord::Base 
    belongs_to :user 
end 

所以,我可以取,我跟隨用戶:User.find(1).following_users

但是如何獲取關注用戶的帖子?喜歡的東西User.find(1).following_users.posts

+0

你想在一個單一的集合所有的職位?或者你需要以某種方式分組給用戶的帖子? – tomcocca

+0

我需要將所有帖子作爲一個集合進行分組 – Kir

回答

2

User.find(1).following_users剛剛返回和AREL參考,在這裏看到:

https://github.com/tcocca/acts_as_follower/blob/master/lib/acts_as_follower/follower.rb#L59

所以,

User.find(1).following_users.includes(:posts) 

應包括在用戶的帖子該查詢,但是這將返回一個用戶數組。以下應工作,遍歷返回的用戶,並收集他們的崗位到一個數組

posts = User.find(1).following_users.includes(:posts).collect{|u| u.posts}.flatten 
0
User.following_users.collect{|u| u.posts} 

這應該工作