jeudi 19 novembre 2015

Fetching post in 2 order, active post ascending and deactive post in descending order

I have Post model in which I have a deleted_at attribute which is used to identify that this post is active or deactive.

Post.order('deleted_at DESC, DATE(posts.created_at)').collect{|q| p [q.id, q.created_at, q.deleted_at]}

This is output:

[3715, Wed, 18 Nov 2015 15:54:57 GMT +00:00, Wed, 18 Nov 2015 17:40:07 GMT +00:00]
[3717, Wed, 18 Nov 2015 15:57:08 GMT +00:00, Wed, 18 Nov 2015 17:38:10 GMT +00:00]
[3716, Wed, 18 Nov 2015 15:56:48 GMT +00:00, nil]
[3718, Wed, 18 Nov 2015 15:57:26 GMT +00:00, nil]
[3719, Wed, 18 Nov 2015 15:57:36 GMT +00:00, nil]
[3720, Wed, 18 Nov 2015 16:04:27 GMT +00:00, nil]
[3721, Wed, 18 Nov 2015 16:04:42 GMT +00:00, nil]
[3722, Wed, 18 Nov 2015 16:04:57 GMT +00:00, nil]

But I want like this

[3716, Wed, 18 Nov 2015 15:56:48 GMT +00:00, nil]
[3718, Wed, 18 Nov 2015 15:57:26 GMT +00:00, nil]
[3719, Wed, 18 Nov 2015 15:57:36 GMT +00:00, nil]
[3720, Wed, 18 Nov 2015 16:04:27 GMT +00:00, nil]
[3721, Wed, 18 Nov 2015 16:04:42 GMT +00:00, nil]
[3722, Wed, 18 Nov 2015 16:04:57 GMT +00:00, nil]
[3715, Wed, 18 Nov 2015 15:54:57 GMT +00:00, Wed, 18 Nov 2015 17:40:07 GMT +00:00]
[3717, Wed, 18 Nov 2015 15:57:08 GMT +00:00, Wed, 18 Nov 2015 17:38:10 GMT +00:00]

Means, if post is active then it should appear in ascending order but deactive post should appear in descending order(most recent deactive post comes first).

Aucun commentaire:

Enregistrer un commentaire