combine results from two queries and order by created_at? [rails 3]

前端 未结 2 559
北荒
北荒 2020-12-14 19:30

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.

For example:

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 19:57

    I believe it should be as simple as:

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

提交回复
热议问题