制作新闻源的最佳方式是什么?
目前我有一个观察者,每当有人创建一个记录时,它就会创建一个新的newsfeedactivity记录。但是,为了处理隐私,我最终有7或8个连接来获得适当的输出。
这似乎是缓慢而低效的。将正确的新闻馈送记录作为范围拉出来的另一种策略是什么?
更多详情:
目前,我有一个网站可以帮助用户跟踪他们正在处理的项目。有公共和私人项目(人们可以邀请合作者)。
我希望我的新闻源包括创建公共项目的时间。当您被邀请参加私人项目时。当用户关注项目时。然后是您正在关注的其他用户的所有操作。然后,对于私有项目,我有另一个连接表来确定谁有权访问这些项目。 (我还希望在新闻源中显示每个项目的评论。)
以下所有关系目前都在连接表中,这就是我有很多连接的原因。
要了解查询类型 - 我认为它看起来像这样:
SELECT
news_feed_activities
。* FROMnews_feed_activities
LEFT JOINuser_following_relationships
ONuser_following_relationships
。following_id =news_feed_activities
。user_id LEFT JOINuser_project_relationships
ONuser_project_relationships
。project_id =news_feed_activities
。respond_to_id ANDnews_feed_activities
.response_to_type ='Project'WHERE (user_following_relationships
。user_id = 1或user_project_relationships
。user_id = 1或news_feed_activities
。user_id = 1或up2
。user_id = 1)GROUP BYnews_feed_activities
。id ORDER BYnews_feed_activities
。id DESC
编辑: 我想我可能最终会沿着http://blog.waxman.me/how-to-build-a-fast-news-feed-in-redis
这些行使用Redis答案 0 :(得分:0)
作为RoR。
在您的控制器中:
@user = current_user # (?)
recent_since = 24.hours.ago
@news_feed = []
# 1) I want my newsfeed to include when public projects are created.
@news_feed += Project.recent.open
# 2) When you are invited to a private project.
@news_feed += @user.invites.received.pending
# 3) When a user follows a project.
@news_feed += @user.user_following_relationships.recent
# 4) And then all of the actions of the other users that you're following.
@news_feed += @user.follows.collect(&:activities)
# 5) Then for the private projects I have another join table to determine who has access to the projects. (There are also comments on each of these projects that I want to show up in the newsfeed as well).
@news_feed += @user.projects.closed
@news_feed.sort!{ |a,b| a.created_at <=> b.created_at }
我也为你做了一些示例范围。 project.rb
scope :recent, :conditions => ["created_at >= ?", 24.hours.ago]
scope :open, :conditions => "publicity = 'Public'"
scope :closed, :conditions => "publicity = 'Private'"
这是基于这样的说法,即您的新闻Feed实际上是模型中最近活动的摘要,而不是“新闻源”模型。