2013-12-17 37 views
0

我目前正在關注從頭集407 活動飼料瑞安貝茨教程,我想知道如何才能顯示我的朋友的活動只。如何顯示用戶的公共活動只有朋友在Rails 4

如果使用的是公共活動的寶石,當我這樣做

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

,併爲這個表是即寶石

class CreateActivities < ActiveRecord::Migration 
    # Create table 
    def self.up 
    create_table :activities do |t| 
     t.belongs_to :trackable, :polymorphic => true 
     t.belongs_to :owner, :polymorphic => true 
     t.string :key 
     t.text :parameters 
     t.belongs_to :recipient, :polymorphic => true 

     t.timestamps 
    end 

    add_index :activities, [:trackable_id, :trackable_type] 
    add_index :activities, [:owner_id, :owner_type] 
    add_index :activities, [:recipient_id, :recipient_type] 
    end 
    # Drop table 
    def self.down 
    drop_table :activities 
    end 
end 

而從頭開始實現這個表是這樣的

class CreateActivities < ActiveRecord::Migration 
    def change 
    create_table :activities do |t| 
     t.belongs_to :user 
     t.string :action 
     t.belongs_to :trackable 
     t.string :trackable_type 

     t.timestamps 
    end 
    add_index :activities, :user_id 
    add_index :activities, :trackable_id 
    end 
end 

如何向用戶朋友顯示此活動?

回答

1

這對我有效。請讓我知道這對你有沒有用。如果不是,我會嘗試修改它。請記住,此示例顯示當前用戶的關注者活動。如果您的應用涉及朋友而不是關注者,則必須對friend_ids進行相應更改

PublicActivity::Activity.order("created_at desc").where(owner_type: "User", owner_id: current_user.followed_users.map {|u| u.id}).all 
相關問題