0

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?

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.