我目前正在关注从第407集开始的活动饲料的ryan bates教程 我想知道如何才能展示我朋友的活动。
如果在使用公共活动gem时我会这样做
@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
如何向用户朋友显示此活动?
答案 0 :(得分:1)
这对我有用。请让我知道这对你有没有用。如果没有,我会尝试修改它。请记住,此示例显示当前用户的关注者活动。如果您的应用涉及朋友,而不是关注者,则必须对friend_ids进行适当的更改
PublicActivity::Activity.order("created_at desc").where(owner_type: "User", owner_id: current_user.followed_users.map {|u| u.id}).all