Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 按数据库中的出现次数对记录排序_Ruby On Rails_Sqlite_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 按数据库中的出现次数对记录排序

Ruby on rails 按数据库中的出现次数对记录排序,ruby-on-rails,sqlite,ruby-on-rails-4,Ruby On Rails,Sqlite,Ruby On Rails 4,在Rails 4.0中,我有一个表,我想根据location_id的出现次数对它进行排序。在本例中,最流行的location_id是3。我想从顶部位置为3的所有记录开始,按降序对表进行排序 表1地质标志- ┌────┬─────────┬─────────────┬─────────────────────┬────────────────────┬─────────────────────┐ │ id │ user_id │ location_id │ message

在Rails 4.0中,我有一个表,我想根据location_id的出现次数对它进行排序。在本例中,最流行的location_id是3。我想从顶部位置为3的所有记录开始,按降序对表进行排序

表1地质标志-

┌────┬─────────┬─────────────┬─────────────────────┬────────────────────┬─────────────────────┐
│ id │ user_id │ location_id │ message             │ created_at         │ updated_at          │
├────┼─────────┼─────────────┼─────────────────────┼────────────────────┼─────────────────────┤
│ 1  ╎ 14      ╎ 2           ╎ Test test t         ╎ 2013-07-23 13:3... ╎ 2013-07-23 13:37... │
│ 2  ╎ 12      ╎ 3           ╎                     ╎ 2013-07-23 13:5... ╎ 2013-07-23 13:54... │
│ 3  ╎ 15      ╎ 2           ╎ <html lang="en" ... ╎ 2013-07-25 08:5... ╎ 2013-07-25 08:57... │
│ 4  ╎ 12      ╎ 3           ╎ Ohohohooh           ╎ 2013-07-25 11:3... ╎ 2013-07-25 11:37... │
│ 5  ╎ 12      ╎ 3           ╎ Pff kjasakljdfas... ╎ 2013-07-25 11:3... ╎ 2013-07-25 11:37... │
│ 6  ╎ 15      ╎ 4           ╎                     ╎ 2013-07-25 13:5... ╎ 2013-07-25 13:54... │
│ 7  ╎ 13      ╎ 4           ╎ dasfdasfdasfdasf    ╎ 2013-07-25 14:3... ╎ 2013-07-25 14:30... │
│ 8  ╎ 13      ╎ 3           ╎                     ╎ 2013-07-25 14:3... ╎ 2013-07-25 14:30... │
│ 9  ╎ 13      ╎ 2           ╎ Test check message  ╎ 2013-07-25 14:3... ╎ 2013-07-25 14:31... │
│ 10 ╎ 13      ╎ 5           ╎ asdfdasfdasfdasfa   ╎ 2013-07-25 14:4... ╎ 2013-07-25 14:42... │
│ 11 ╎ 16      ╎ 7           ╎ Hohohooh            ╎ 2013-07-26 07:5... ╎ 2013-07-26 07:50... │
└────┴─────────┴─────────────┴─────────────────────┴────────────────────┴─────────────────────┘
我知道如何通过例如:geogocecks.wherelocation\u id:3.count=>4来计算它们,并考虑了一个循环。where.count请求贯穿整个表,然后比较结果以找到最大的一个,但我非常确定有一个更聪明的解决方案


所需输出:所需输出应该是按降序排列的位置id列表。在这种情况下,它应该是:3,2,4,5,7,我认为它应该工作:

Table.find(:all, :order => "location_id_count DESC")

因此,正如我在一篇评论中指出的那样,使用SQL进行操作将相当复杂,并且需要对SQL有很好的了解。但是在rails c中花了一点时间,我找到了一种在rails中实现它的方法! 此查询的结果将是位置而不是地球化学标记,但我认为这不会对您造成问题

Location.joins(:geochecks).order('COUNT(geochecks.id) DESC').group('location.id')

我认为您需要有一个SQL查询,首先执行分组、计数和排序,然后将此查询的结果再次连接到表以将其用作顺序。我对SQL查询不是很了解。你能给我举一些例子吗?谢谢,这就是重点。如果您不知道如何做,我建议您先学习SQL。做你不懂的事情不是一条路。你可以试着用Rails来执行这个,让我写一个答案。嗯,好的,如果你加入了其他的表,这可能是基于B的计数在表a的Rails顺序记录中,但我不知道如何在没有自连接的情况下在单个表上实现。明白了!当我尝试此操作时,控制台返回错误:SQLite3::SQLException:没有这样的列:location\u id\u count。所以它不明白_count是什么意思,它只是以默认顺序返回了我的位置表。我不太确定对GechecksI排序有什么帮助。我只是想根据每个位置出现的次数降序输出一个位置ID列表。我将把所需的输出放在上面。好的,如果您运行Location.joins:geogocecks.order'countgeogocecks.id DESC'。group'Location.id'。map&:geogocecks.map&:count,您会得到一个降序值数组吗?请在评论中添加此结果!哦,是的,现在我明白了。Thanksher是以防万一的结果-=>[4,3,2,2,1]