我正在使用 Public Activity Gem 来创建各种通知系统。在陈述我的问题之前,我将提供相关代码:
活动的控制器操作:
def index
@activities1 = PublicActivity::Activity.where(owner_id: current_user.articles, owner_type: "Article" )
@activities2 = PublicActivity::Activity.where(owner_id: current_user.id , owner_type: "User")
@activities = @activities1 + @activities2
@activities.sort_by{ |a| a[:created_at] }
end
活动视图#index
<% @activities.each do |activity| %>
<%= render_activity activity %>
<% end %>
我的目标是收集我想在索引视图上显示的所有相关活动。但是,会发生以下情况:
1) Activity filler text(Created June 1) (Owner: Article)
2) Activity filler text (Created June 3) (Owner: Article)
3) Activity filler text (Created June 4) (Owner: Article)
4) Activity filler text (Created June 1) (Owner: User)
5) Activity filler text (created June 2) (Owner: User)
首先渲染拥有者文章的活动,然后渲染拥有者用户的活动。我希望它们都按 order(:created_at) 呈现,最好是按降序呈现,尽管它们的所有者类型是什么。有没有更好的方法来实现这一目标?