Kadismile
Kadismile

Reputation: 208

How to display public activity of the user only friends in Rails 4

I am currently following ryan bates tutorial on activity feed from scratch episode 407 and I was wondering how can I display the activity of my friends only.

If when using the public activity gem I do this

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

and the table for this is i.e. the gem

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

And for implementing this from scratch the table is this

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

How do I display this activity to users friends?

Upvotes: 0

Views: 1203

Answers (1)

Justin
Justin

Reputation: 4940

This worked for me. Let me know if it works for you. If not, I'll try to revise it. Keep in mind, this example shows the current user's followers activities. If your app involved friends, not followers, you'll have to make the appropriate change to friend_ids

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

Upvotes: 1

Related Questions