通过created_at组合来自两个查询的结果与秩序? [轨道3]组合、轨道、秩序、两个

2023-09-08 16:02:48 作者:我深知我不配

寻找利用活动记录,以抓住从两款车型的数据,结合数据,然后通过created_at的组合输出排序的简单方法。

Looking for a simple method utilizing active record to grab data from two models, combine the data, and then sort the combined output by created_at.

例如:

假设两种型号,评论和放大器;都喜欢belongs_to的用户

assume two models, Comment & Like both belongs_to User

返回@用户的意见和喜好按日期排列组合列表

return a combined list of @user's comments and likes sorted by date

我知道我可以在SQL这样做,但我真的很喜欢的活动记录的解决方案。

I know I can do this in SQL but I'd really like an active record solution.

谢谢!

推荐答案

我相信这应该是简单的:

I believe it should be as simple as:

combined_sorted = (User.comments + User.likes).sort{|a,b| a.created_at <=> b.created_at }