Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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/9/ruby-on-rails-3/4.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 postgres偏移/限制是否与mysql的工作方式不同?_Ruby On Rails_Ruby On Rails 3_Postgresql_Postgis - Fatal编程技术网

Ruby on rails postgres偏移/限制是否与mysql的工作方式不同?

Ruby on rails postgres偏移/限制是否与mysql的工作方式不同?,ruby-on-rails,ruby-on-rails-3,postgresql,postgis,Ruby On Rails,Ruby On Rails 3,Postgresql,Postgis,我有一个rails应用程序(postgres+postGIS),带有一个带有评级列(整数)的post模型。如果我进入控制台并执行以下操作: Post.order("rating DESC").map(&:id) => [9, 15, 19, 6, 17, 5, 4, 16, 1, 3, 13, 20, 14, 10, 8, 12, 7, 2, 18, 11] 然而,如果我试图通过限制和偏移一次循环通过这些,我会得到奇怪的结果 Post.order("rating DESC").l

我有一个rails应用程序(postgres+postGIS),带有一个带有评级列(整数)的post模型。如果我进入控制台并执行以下操作:

Post.order("rating DESC").map(&:id)
=> [9, 15, 19, 6, 17, 5, 4, 16, 1, 3, 13, 20, 14, 10, 8, 12, 7, 2, 18, 11]
然而,如果我试图通过限制和偏移一次循环通过这些,我会得到奇怪的结果

Post.order("rating DESC").limit(1).offset(0)
=> [#<Post id: 5, body: "Hi", rating: 4, location: #<RGeo::Geographic::SphericalPointImpl:0x81bb34c0 "POINT (-118.495 34.017)">, user_id: 8, created_at: "2012-07-25 22:43:41", updated_at: "2012-07-25 22:43:41">]
Post.order(“评级描述”)。限制(1)。偏移(0)
=> [#]
为什么那个帖子是5号?应该是#9。无论如何,当我应用偏移量时,它变得更加古怪

>Post.order("rating DESC").limit(1).offset(1)
=> [#<Post id: 5, body: "Hi", rating: 4, location: #<RGeo::Geographic::SphericalPointImpl:0x81bb34c0 "POINT (-118.495 34.017)">, user_id: 8, created_at: "2012-07-25 22:43:41", updated_at: "2012-07-25 22:43:41">]

>Post.order("rating DESC").limit(1).offset(2)
=> [#<Post id: 5, body: "Hi", rating: 4, location: #<RGeo::Geographic::SphericalPointImpl:0x81bb34c0 "POINT (-118.495 34.017)">, user_id: 8, created_at: "2012-07-25 22:43:41", updated_at: "2012-07-25 22:43:41">]

>Post.order("rating DESC").limit(1).offset(3)
=> [#<Post id: 5, body: "Hi", rating: 4, location: #<RGeo::Geographic::SphericalPointImpl:0x81bb34c0 "POINT (-118.495 34.017)">, user_id: 8, created_at: "2012-07-25 22:43:41", updated_at: "2012-07-25 22:43:41">]

>Post.order("rating DESC").limit(1).offset(4)
=> [#<Post id: 15, body: "I luv coffee", rating: 4, flagged: 0, location: #<RGeo::Geographic::SphericalPointImpl:0x82260df4 "POINT (-118.495 34.017)">, user_id: 1, created_at: "2012-07-25 22:43:41", updated_at: "2012-07-25 22:43:41">]
>后订单(“评级描述”).限制(1).偏移(1)
=> [#]
>后订单(“评级说明”)。限额(1)。抵销(2)
=> [#]
>后订单(“评级说明”)。限额(1)。抵销(3)
=> [#]
>后订单(“评级说明”)。限额(1)。抵销(4)
=> [#]

您是否注意到,对于您显示的唯一结果,
评分为4?您在
评级上排序
,没有辅助排序键,因此无法保证在两个不同的调用中会出现什么订单关系,甚至无法保证这些关系的排序方式相同

尝试在您的
订单中添加平局断路器

Post.order('rating DESC, id')
然后将
评级
包含在您正在查看的内容中:

Post.order('rating desc, id').select('id, rating').map { |p| [ p.id, p.rating ] }
Post.order('rating desc, id').select('id, rating').limit(1).offset(3).map { |p| [ p.id, p.rating ] }
#...

这应该会给你合理且一致的结果。

你注意到了吗,
对你显示的唯一结果的评分是4?您在
评级上排序
,没有辅助排序键,因此无法保证在两个不同的调用中会出现什么订单关系,甚至无法保证这些关系的排序方式相同

尝试在您的
订单中添加平局断路器

Post.order('rating DESC, id')
然后将
评级
包含在您正在查看的内容中:

Post.order('rating desc, id').select('id, rating').map { |p| [ p.id, p.rating ] }
Post.order('rating desc, id').select('id, rating').limit(1).offset(3).map { |p| [ p.id, p.rating ] }
#...
这会给你带来明智和一致的结果