简短的回答是:这取决于。如果您只需要最近的活动,而不需要跟踪活动或完整的“活动源”功能,那么 SQL 就是您的最佳选择。但如果您认为需要执行完整的活动提要,您可以为其创建一个模型。
我们最近对我们的项目进行了一次活动。这是我们的建模方式
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中我们做
Class AnswersController
def create
...
Activity.add(current_user, ActivityType::QUESTION_ANSWERED, @answer)
...
end
end
为了从用户那里获取最近的活动列表,我们这样做
@activities = @user.activities