加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails活动从多态跟踪中提供

发布时间:2020-12-17 02:06:05 所属栏目:百科 来源:网络整理
导读:我正在跟踪以下表格: 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) 我目前正在从用户关注的用户那里获得活动,
我正在跟踪以下表格:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读