如何在活动饲料集团类似物品 [英] How to group similar items in an activity feed

查看:165
本文介绍了如何在活动饲料集团类似物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个社交网站,我有你关注的人事件的活动,我想集合相似类型在很短的时间内取得了一起,更紧凑的活动饲料事件。想象一下Facebook的如何显示一个逗号分隔的列表,当你喜欢快速连续的几件事情:乔喜欢啤酒,足球和薯条

For a social network site, I have an activity of events from people you follow, and I'd like to group similar types of events made within a short timeframe together, for a more compact activity feed. Imagine how Facebook displays a comma separated list when you 'like' several things in rapid succession: 'Joe likes beer, football and chips.'

我了解使用的ActiveRecord的可枚举结果GROUP_BY方法,但需要有做填充的属性,我可以按更高一些初步的工作。我的问题处理这两种存储活动数据的方式,这些集团可以标记,然后稍后再检索它们。

I understand using the group_by method on ActiveRecord Enumerable results, but there needs to be some initial work done populating a property that I can group by later. My questions deal with both storing activity data in a way that these groupings can be marked, and then later retrieving them again.

现在我有一个活动的模式,这是犯了活动,而该项目的,它的上面挂(在我的例子中,假设'啤酒','足球'和'芯片'是用户之间的联接协会一个像模型的记录)。还有其他的活动类型除了喜欢太(活动,节约您的收藏夹,等等)。什么我考虑是,因为该关联被创建,当该类型的最后协会已完成进行检查,并且如果它被超过一定时间周期前作出更,递增一个'活动块计数器,它是部分的活动模式。后来,使这项活动饲料的时候,我可以按用户,然后键入,那么这个活动块计数器。

Right now I have an Activity model, which is a join association between the user that committed the activity and the item that that it's linked to (in my example above, assume 'beer', 'football' and 'chips' are records of a Like model). There are other activity types aside from 'likes' too (events, saving favorites, etc). What I'm considering is, as this association is created, a check is made when the last association of that type was done, and if it was made more than a certain time period ago, incrementing an 'activity block' counter that is part of the Activity model. Later, when rendering this activity feed, I can group by user, then type, then this activity block counter.

例子:比方说,更新的2块在同一天进行。用户喜欢两件事,在2:05以后3更多的事情,在5:45。之后的第三次更新(第2块的开始)发生在5点45分,该模型检测了太多的时间已经过去了,并增加了1活性块计数器,从而迫使任何后续更新到一个新的块时,他们通过一个被渲染GROUP_BY电话:

Example: Let's say 2 blocks of updates are made within the same day. A user likes 2 things at 2:05 and later 3 more things at 5:45. After the third update (the start of the 2nd block) happens at 5:45, the model detects too much time has passed and increments its activity block counter by 1, thus forcing any following updates into a new block when they are rendered via a group_by call:

2:05 Joe likes beer nuts and Hooters.

5:45 Joe likes couches, chips and salsa.

7:00 Joe is attending the Football Viewing Party At Joe's

我的第一个问题:什么是增加这样一个计数器的有效途径?它不再AUTO_INCREMENT,所以我能想到的最简单的事情是看柜台的最后一个记录作为参考点。但是,这不能从选中时作出这种类型的最后更新相同的查询,因为另一种类型的更高版本的更新可能已经接收到下一个计数器的值。他们不必是全局唯一的,但是这将是很好的。

My first question: What's an efficient way to increment a counter like this? It's no longer auto_increment, so the easiest thing I can think of is looking at the counter for the last record as a reference point. However, this couldn't be from the same query that checked for when the last update of that type was made, since a later update of another type could have already received the next counter value. They don't have to be globally unique, but that would be nice.

其他的整体策略,我想的是另一种模式被称为ActivityBlock,联接类似活动的组织在一起。在许多情况下,更新将自行分离的,所以这似乎有点低效率的一个记录为每个单独的活动

The other overall strategy I thought of was another model Called ActivityBlock, that joins groups of similar activities together. In many cases, updates will be isolated by themselves though, so this seems a little inefficient to have one record for each individual activity.

执行下列任一这些看起来像一个坚实的战略是什么?

Do either of these seem like a solid strategy?

我的最后一个问题分页左右旋转。现在,我们正在处理模块,它很难总是准确地显示了一定的条目,分页踢之前,要么一个人(隔离)活动的更新,还是那么块应该算作只是1,所以在最低我GROUP_BY层,我可以将一个计数器来跟踪多少行我已经显示出来,但这意味着我不能只是做一个数据库查询了,只是指定限制声明。有什么办法我仍然可以做到这一点不需要重复执行额外的SQL查询,直到我达到了我的页数限制?

My final question revolves around pagination. Now that we're dealing with blocks, it's harder to always display exactly a certain amount of entries, before pagination kicks in. Either an individual (isolated) Activity update, or a block of then should count as just 1, so at the lowest layer of my group_by, I can incorporate a counter to track how many rows I've displayed, but this means I can't just make one DB query anymore and simply specify a limit statement. Is there any way I could still do this without repeatedly performing additional SQL queries until I've reached my page limit?

这是一个优点ActivityBlock模型方法,因为我可以很容易施加限制呼叫的,并阻止可能包含一个自动递增计数器为好。

This would be one advantage of the ActivityBlock model approach, since I could easily apply a limit call to that, and blocks could contain an auto increment counter as well.

推荐答案

查看的http:// railscasts.com/episodes/406-public-activity 他还贴出一个在如何从头开始做,在情节407(这是一个专业的情节虽然)。

Check out http://railscasts.com/episodes/406-public-activity He also posted one on how to do it from scratch in episode 407 (it's a Pro episode though).

这篇关于如何在活动饲料集团类似物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆