开发者

How would you do a find on two different models and sort both in one array by created_at column

开发者 https://www.devze.com 2022-12-10 17:46 出处:网络
So for example ... Lets say I have a Posts model and a News model, that are essentially the same thing (structure wise)

So for example ...

Lets say I have a Posts model and a News model, that are essentially the same thing (structure wise)

columns are

* title

* content

* createdat

* updatedat

The client has requested showing both news and Posts on the same page interleaved and sorted by created开发者_Go百科_at date ...

Does anyone know how I would go about doing something like this?


(Post.all + News.all).sort_by(&:created_at)


Just a warning, cwninja's answer will be really slow with large datasets because it requires pulling ALL of the members out of the database and then running a sort on them.

I would rarely suggest raw SQL, but in this case I can't think of a better way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号