Sql 如何通过Rails模型在Postgres上按日期分组

Sql 如何通过Rails模型在Postgres上按日期分组,sql,ruby-on-rails,postgresql,activerecord,arel,Sql,Ruby On Rails,Postgresql,Activerecord,Arel,我将Postgres与Ruby on Rails结合使用,并希望在away中查询我的posts表,该表返回按创建日期分组的所有posts,每组按时间顺序排列 我的目标的一个完美例子是,任何一个博客的索引都会按日期列出所有帖子 我希望得到的伪代码对象结构类似于: posts_grouped_by_date = { "1/2/2014" => [post1, post2, post3], "1/15/2014" => [post5, post6,post7], etc.. }

我将Postgres与Ruby on Rails结合使用,并希望在away中查询我的
posts
表,该表返回按创建日期分组的所有posts,每组按时间顺序排列

我的目标的一个完美例子是,任何一个博客的索引都会按日期列出所有帖子

我希望得到的伪代码对象结构类似于:

posts_grouped_by_date = {
  "1/2/2014" => [post1, post2, post3],
  "1/15/2014" => [post5, post6,post7],
  etc..
}
我的表名为
posts
,如下所示:

 id |                               title                               |         created_at
----+-------------------------------------------------------------------+----------------------------
 13 | Eaque hic tenetur at quam quibusdam eveniet veritatis.            | 2014-08-18 02:40:31.329665
  9 | The only thing i want to say.                                     | 2014-08-18 02:40:31.329665
 16 | Est sunt cumque rerum nam nobis consequuntur aut.                 | 2014-08-19 02:58:26.817936
 18 | Fuga assumenda facilis aut mollitia eum soluta.                   | 2014-08-19 02:58:26.837839
  8 | The only thing i want to say.                                     | 2014-08-18 02:40:31.329665
 12 | Laudantium dolores odit sed veritatis in et vel deserunt.         | 2014-08-18 02:40:31.329665
 10 | The only thing i want to say.                                     | 2014-08-18 02:40:31.329665
 20 | Rem laboriosam ipsam sunt maxime quia adipisci et voluptatem.     | 2014-08-19 02:58:26.837839
 19 | Laudantium sit explicabo et quia eligendi ipsam.                  | 2014-08-19 02:58:26.837839
 21 | Repellat qui eos dolorem.                                         | 2014-08-19 02:58:26.837839
  6 | The only thing i want to say.                                     | 2014-08-18 02:40:31.329665
 17 | Error voluptatem architecto distinctio impedit iste dolores enim. | 2014-08-19 02:58:26.829968
 15 | Vero modi voluptate deleniti labore quis est.                     | 2014-08-19 02:58:26.808126
 14 | Et neque porro qui animi facilis numquam.                         | 2014-08-19 02:58:26.800474
Post.order(:created_at).group_by {|p| p.created_at.to_date}

我将如何使用ActiveRecord或Arel语法来实现这一点?代码背后的解释也会非常有用。我用它来学习/练习,而不仅仅是复制/粘贴。

我想你想要这样的东西:

 id |                               title                               |         created_at
----+-------------------------------------------------------------------+----------------------------
 13 | Eaque hic tenetur at quam quibusdam eveniet veritatis.            | 2014-08-18 02:40:31.329665
  9 | The only thing i want to say.                                     | 2014-08-18 02:40:31.329665
 16 | Est sunt cumque rerum nam nobis consequuntur aut.                 | 2014-08-19 02:58:26.817936
 18 | Fuga assumenda facilis aut mollitia eum soluta.                   | 2014-08-19 02:58:26.837839
  8 | The only thing i want to say.                                     | 2014-08-18 02:40:31.329665
 12 | Laudantium dolores odit sed veritatis in et vel deserunt.         | 2014-08-18 02:40:31.329665
 10 | The only thing i want to say.                                     | 2014-08-18 02:40:31.329665
 20 | Rem laboriosam ipsam sunt maxime quia adipisci et voluptatem.     | 2014-08-19 02:58:26.837839
 19 | Laudantium sit explicabo et quia eligendi ipsam.                  | 2014-08-19 02:58:26.837839
 21 | Repellat qui eos dolorem.                                         | 2014-08-19 02:58:26.837839
  6 | The only thing i want to say.                                     | 2014-08-18 02:40:31.329665
 17 | Error voluptatem architecto distinctio impedit iste dolores enim. | 2014-08-19 02:58:26.829968
 15 | Vero modi voluptate deleniti labore quis est.                     | 2014-08-19 02:58:26.808126
 14 | Et neque porro qui animi facilis numquam.                         | 2014-08-19 02:58:26.800474
Post.order(:created_at).group_by {|p| p.created_at.to_date}
1-Post.order(:created_at)通过created_at属性按升序排列帖子


2-然后,一旦你订购了所有的帖子,你就可以使用块中的代码对它们进行分组。由于您不希望时间戳包含在分组中(每秒钟都有不同的记录,这有什么意义呢?),因此您可以将时间戳转换为日期。

这是可行的,但需要在运行
group\u by
之前将所有内容都放入Ruby中。难道没有办法让SQL server完成这项工作吗?嗯,您必须使用数据库中的本机函数。我不知道SQL Server,但这里有一个人用MYSql解决了这个问题(仅供参考)