制作新闻提要的最佳方法是什么?
目前我有一个观察者,每当有人创建一个记录时,它都会创建一个新的newsfeedactivity记录。但是,为了处理隐私,我最终有7或8个连接来获得适当的输出。
看来这将是缓慢和低效的。还有什么策略可以将正确的newsfeedactivity记录作为作用域提取出来?
更多细节:
目前我有一个网站,帮助用户跟踪他们正在进行的项目。有公共和私人项目(人们可以邀请合作者)。
我希望在创建公共项目时包含我的新闻提要。当你被邀请参加一个私人项目时。当用户跟踪项目时。然后是您正在跟踪的其他用户的所有操作。然后对于私有项目,我有另一个连接表来确定谁有权访问这些项目。(也有关于这些项目的评论,我也希望在新闻提要中出现)。
以下所有关系当前都在联接表中,这就是为什么我有很多联接。
要了解查询的类型,我想它应该是这样的:
news_feed_activities左连接中选择news_feed_activities*
user_following_relationships打开
user_following_relationships。遵循id=
news_feed_activities。用户id左联接
user_project_relationships打开
user_project_relationships。项目id=
news_feed_activities。对id和
news_feed_activities。对“项目”类型的响应,其中
user_following_relationships。用户id=1或
user_project_relationships。用户id=1或
news_feed_activities。用户id=1或
up2.user\u id=1)分组依据news_feed_activities.id排序依据
news_feed_activities.id描述
编辑:
我想我最终可能会使用Redis

最佳答案

作为罗尔。
在控制器中:

@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 }

我也给你做了一些样本。
项目.rb
scope :recent, :conditions => ["created_at >= ?", 24.hours.ago]
scope :open, :conditions => "publicity = 'Public'"
scope :closed, :conditions => "publicity = 'Private'"

这是基于这样一个原则:你的新闻提要实际上是模型之间最近活动的摘要,而不是有一个“新闻提要”模型。

关于mysql - 如何在没有很多加入声明的情况下制作新闻提要,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9289100/

10-11 22:22
查看更多