当我拥有done this before 时,我已经通过使用非规范化的UserActivity 模型或与belongs_to 多态关联到ActivitySource 的类似模型来管理它 - 这可以是您想要的任何类型的内容显示(帖子、cmets、赞成票、点赞等等……)。
然后,当创建任何要显示的实体时,您将有一个 Observer 触发并在 UserActivity 表中创建一行,其中包含指向记录的链接。
然后要显示列表,您只需查询UserActivity 按created_at 降序排序,然后通过多态activity_source 关联导航以获取内容数据。然后,您需要在视图模板中使用一些智能来呈现 cmets 和帖子以及其他任何不同的内容。
例如类似...
user_activity.rb:
class UserActivity < ActiveRecord::Base
belongs_to :activity_source, :polymorphic => true
# awesomeness continues here...
end
comment.rb (post/whatever)
class Comment < ActiveRecord::Base
# comment awesomeness here...
end
activity_source_observer.rb
class ActivitySourceObserver < ActiveRecord::Observer
observe :comment, :post
def after_create(activity_source)
UserActivity.create!(
:user => activity_source.user,
:activity_source_id => activity_source.id,
:activity_source_type => activity_source.class.to_s,
:created_at => activity_source.created_at,
:updated_at => activity_source.updated_at)
end
def before_destroy(activity_source)
UserActivity.destroy_all(:activity_source_id => activity_source.id)
end
end