2013-03-08 34 views
2

我在應用程序public_activity和acts_as_follower中使用了兩個gem。我有act_as_follower設置罰款,用戶可以跟隨另一個用戶以及樂隊。Rails public_activity feed with polymorphic following

問題是將饋送從您所關注的對象中取消。使用公共活動,我有一個使用推薦方法的全局提要:

@activities = PublicActivity::Activity.order("created_at desc") 

工作正常。不過,我想也有以下的飼料,這將reqiure沿着線的東西:

@activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.all_following, owner_type: ????????) 

不指定只使用owner_id這是一個問題,因爲用戶和帶可以有owner_type,創建飼料相同的ID。在這種情況下我如何指定所有者類型?

回答

0

歐文,在這裏你使用所謂的polymorphic associations試圖找出他們如何工作。 它只是將數據庫放入數據庫中,不僅僅是id,而是對象的類型。

1

對於用戶:

@activities = PublicActivity::Activity.where(owner_id: current_user.follows_by_type('User'), owner_type: 'User').order("created_at desc") 

對於波段:

@activities = PublicActivity::Activity.where(owner_id: current_user.follows_by_type('Band'), owner_type: 'Band').order("created_at desc") 

對於混合物:

@activities = PublicActivity::Activity.where("(owner_id IN (?) AND owner_type = 'User') or (owner_id IN (?) AND owner_type = 'Band')", current_user.follows_by_type('User'), current_user.follows_by_type('Band')).order("created_at desc") 
相關問題