Ruby on Rails 的近期活动

编程入门 行业动态 更新时间:2024-10-24 12:20:18
本文介绍了Ruby on Rails 的近期活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

实现 StackOverflow 风格的近期活动"页面的最佳方式是什么?

What is the best way to implement a StackOverflow-style Recent Activities page?

我有一个包含用户照片的图库,我希望在其他用户对他们的照片发表评论或投票时通知他们.

I have a Gallery with user's photos and I want them to be notified when other users comment or vote their photos.

我应该创建一个包含最近活动的新表(每当用户发表评论或投票时更新)还是应该简单地使用 MySQL 查询?

Should I create a new table which contains recent activities (updated whenever a user posts a comment or votes) or should I simply use a MySQL query?

推荐答案

简短的回答是:视情况而定.如果您只需要最近的活动并且不需要跟踪活动或完整的活动提要"功能,那么 SQL 是您的最佳选择.但是如果您认为需要做一个完整的活动提要类型的事情,您可以为其创建一个模型.

The short answer is : it depends. If you only need most recent activities and don't need to keep track of activities, or a full 'activity feed' feature, SQL is the way to go. but if you see the need to do a full activity feed kind thing, You can create a model for it.

我们最近在我们的项目上做了一个活动流.这是我们如何建模

We did a activity stream recently on our project. Here is how we modeled it

Class Activity belongs_to :user_activities # all the people that cares about the activity belongs_to :actor, class_name='user' # the actor that cause the activity belongs_to :target, :polymorphic => true # the activity target(e.g. if you vote an answer, the target can be the answer ) belongs_to :subtarget, :polymorphic => true # the we added this later since we feel the need for a secondary target, e.g if you rate an answer, target is your answer, secondary target is your rating. def self.add(actor, activity_type, target, subtarget = nil) activity = Activity.new(:actor => actor, :activity_type => activity_type, :target => target, :subtarget => subtarget) activity.save! activity end end

在 answer_controller 我们做

in the answer_controller we do

Class AnswersController def create ... Activity.add(current_user, ActivityType::QUESTION_ANSWERED, @answer) ... end end

要从用户那里获取最近的活动列表,我们会这样做

To get a recent activity list from a user we do

@activities = @user.activities

更多推荐

Ruby on Rails 的近期活动

本文发布于:2023-10-27 16:03:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533865.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:近期   Ruby   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!