我正在跟踪以下表格:
Story (id,user_id,content) Vote (id,story_id) Flag (id,story_id) etc..
有活动表:
Activity (id,action,trackable_id,trackable_type)
关系表:
Relationship (id,follower_id,followed_id)
def get_activity_from_followers(current_user) followed_user_ids = "SELECT followed_id FROM relationships WHERE follower_id = :user_id" where("user_id IN (#{followed_user_ids})",user_id: user.id) end
我的问题是,我如何获得可跟踪表格(例如故事,投票,旗帜)所属的活动.
所以现在我得到的东西是这样的:
我也希望得到这样的东西:
>“你不跟随的人”投了你的故事
>“你不跟随的人”标记了你的故事
我该怎么做?谢谢.
解决方法
我建议你有以下类定义和关联:
# User class User :trackable end # Flag (id,story_id) class Flag :trackable end # with an activity table: # Activity (id,trackable_type) class Activity true end # The relationship table: # Relationship (id,following_id) class Relationship "User" belongs_to :following,:class_name => "User" end
现在,让我们找到您关注的用户的活动:
# List activities of my followings Activity.where(:user_id => current_user.followings)
列出我的活动
current_user.activities