Rails分页重复行

时间:2014-07-30 18:10:47

标签: ruby-on-rails postgresql pagination will-paginate

当我使用偏移量0和50触发sql查询时,我得到重复的条目。就业有很多反馈。数据库是PG。我也尝试过feedbacks.uuid和employments.uuid排序,但没有成功。请问哪里有捕获?令我更困惑的是,前两页(偏移0和25)的排序是可以的。第三页搞砸了并包含第1页的条目。但是如果我没有限制和偏移来激活sql,一切都还可以。只有在我进行分页时才会出现问题。

all_staff = Employment.includes(:feedbacks, :user, venue: [:venue_account]).with_roles(1)
            .where("CONCAT(employments.first_name, ' ', employments.last_name) LIKE ?", name).references(:employments)

@staff = all_staff.reorder("feedbacks.updated_at DESC NULLS LAST").references(:feedbacks).limit(25).offset(0)

解雇SQL查询:

SELECT COUNT(*) FROM "employments" WHERE "employments"."role" = 1 AND (CONCAT(employments.first_name, ' ', employments.last_name) LIKE '%%')

SELECT DISTINCT "employments"."uuid", feedbacks.updated_at AS alias_0 FROM "employments" LEFT OUTER JOIN "feedbacks" ON "feedbacks"."employment_id" = "employments"."uuid" LEFT OUTER JOIN "users" ON "users"."uuid" = "employments"."user_id" LEFT OUTER JOIN "venues" ON "venues"."uuid" = "employments"."venue_id" LEFT OUTER JOIN "venue_accounts" ON "venue_accounts"."venue_id" = "venues"."uuid" WHERE "employments"."role" = 1 AND (CONCAT(employments.first_name, ' ', employments.last_name) LIKE '%%') ORDER BY feedbacks.updated_at DESC NULLS LAST LIMIT 25 OFFSET 0

SELECT "employments"."uuid" AS t0_r0, "employments"."first_name" AS t0_r1, "employments"."last_name" AS t0_r2, "employments"."picture" AS t0_r3, "employments"."state" AS t0_r4, "employments"."about" AS t0_r5, "employments"."visible" AS t0_r6, "employments"."role" AS t0_r7, "employments"."position" AS t0_r8, "employments"."kpi" AS t0_r9, "employments"."kfi" AS t0_r10, "employments"."venue_id" AS t0_r11, "employments"."user_id" AS t0_r12, "employments"."started_at" AS t0_r13, "employments"."ended_at" AS t0_r14, "employments"."kpi_updated_at" AS t0_r15, "employments"."created_at" AS t0_r16, "employments"."updated_at" AS t0_r17, "feedbacks"."uuid" AS t1_r0, "feedbacks"."text" AS t1_r1, "feedbacks"."last_manager_conversation_id" AS t1_r2, "feedbacks"."last_user_conversation_id" AS t1_r3, "feedbacks"."first_manager_conversation_id" AS t1_r4, "feedbacks"."conversations_count" AS t1_r5, "feedbacks"."state" AS t1_r6, "feedbacks"."visible" AS t1_r7, "feedbacks"."last_manager_reads" AS t1_r8, "feedbacks"."last_user_read_at" AS t1_r9, "feedbacks"."settled_at" AS t1_r10, "feedbacks"."opened_at" AS t1_r11, "feedbacks"."left_at" AS t1_r12, "feedbacks"."last_active_at" AS t1_r13, "feedbacks"."user_id" AS t1_r14, "feedbacks"."employment_id" AS t1_r15, "feedbacks"."venue_id" AS t1_r16, "feedbacks"."created_at" AS t1_r17, "feedbacks"."updated_at" AS t1_r18, "feedbacks"."application_type" AS t1_r19, "users"."uuid" AS t2_r0, "users"."first_name" AS t2_r1, "users"."last_name" AS t2_r2, "users"."email" AS t2_r3, "users"."picture" AS t2_r4, "users"."about" AS t2_r5, "users"."badge" AS t2_r6, "users"."manager_badge" AS t2_r7, "users"."thanks_count" AS t2_r8, "users"."solved_count" AS t2_r9, "users"."not_solved_count" AS t2_r10, "users"."created_at" AS t2_r11, "users"."updated_at" AS t2_r12, "venues"."uuid" AS t3_r0, "venues"."response_rate" AS t3_r1, "venues"."name" AS t3_r2, "venues"."picture" AS t3_r3, "venues"."venue_category_id" AS t3_r4, "venues"."employee_of_day_id" AS t3_r5, "venues"."thanks_count" AS t3_r6, "venues"."latitude" AS t3_r7, "venues"."longitude" AS t3_r8, "venues"."foursquare_id" AS t3_r9, "venues"."foursquare_about" AS t3_r10, "venues"."about" AS t3_r11, "venues"."created_at" AS t3_r12, "venues"."updated_at" AS t3_r13, "venues"."new_venue_category_id" AS t3_r14, "venue_accounts"."uuid" AS t4_r0, "venue_accounts"."status" AS t4_r1, "venue_accounts"."expires_at" AS t4_r2, "venue_accounts"."user_id" AS t4_r3, "venue_accounts"."venue_id" AS t4_r4, "venue_accounts"."visible_new_thanks" AS t4_r5, "venue_accounts"."visible_new_solved_feedback" AS t4_r6, "venue_accounts"."visible_new_not_solved_feedback" AS t4_r7, "venue_accounts"."created_at" AS t4_r8, "venue_accounts"."updated_at" AS t4_r9 FROM "employments" LEFT OUTER JOIN "feedbacks" ON "feedbacks"."employment_id" = "employments"."uuid" LEFT OUTER JOIN "users" ON "users"."uuid" = "employments"."user_id" LEFT OUTER JOIN "venues" ON "venues"."uuid" = "employments"."venue_id" LEFT OUTER JOIN "venue_accounts" ON "venue_accounts"."venue_id" = "venues"."uuid" WHERE "employments"."role" = 1 AND (CONCAT(employments.first_name, ' ', employments.last_name) LIKE '%%') AND "employments"."uuid" IN ('64342810f25749c99dced3e4a94733bb', '090acd676f7b46a29af14865a495bf6b', 'e3b0a2e509fe49d1a018a9b2524bcaab', '42891e3c5f4b4b60b8c6d8009ea92c6f', 'e3b0a2e509fe49d1a018a9b2524bcaab', '8f88a65466a3499a8fe7d2b13d79a7ce', 'e248efe594094c0aaefc578d285d188c', 'e248efe594094c0aaefc578d285d188c', '581bce76e1114ce99e068b59362d39d4', 'd9f9a6eba7184069ae500b430a222294', '39b5e272cd8b4e8e95286f93db2c81a3', 'c9d86838815949d0acfc96d0048eb398', 'd9f9a6eba7184069ae500b430a222294', 'd32a33c77fb94e289a2ef54ed6f34761', 'd9f9a6eba7184069ae500b430a222294', '581bce76e1114ce99e068b59362d39d4', '2849ed41f0254f518dbf0b9551ce1552', '581bce76e1114ce99e068b59362d39d4', '581bce76e1114ce99e068b59362d39d4', 'd9f9a6eba7184069ae500b430a222294', '581bce76e1114ce99e068b59362d39d4', 'd9f9a6eba7184069ae500b430a222294', '39b5e272cd8b4e8e95286f93db2c81a3', 'd32a33c77fb94e289a2ef54ed6f34761', 'd9f9a6eba7184069ae500b430a222294') ORDER BY feedbacks.updated_at DESC NULLS LAST

如果偏移50,则第一个修改为

SELECT DISTINCT "employments"."uuid", feedbacks.updated_at AS alias_0 FROM "employments" LEFT OUTER JOIN "feedbacks" ON "feedbacks"."employment_id" = "employments"."uuid" LEFT OUTER JOIN "users" ON "users"."uuid" = "employments"."user_id" LEFT OUTER JOIN "venues" ON "venues"."uuid" = "employments"."venue_id" LEFT OUTER JOIN "venue_accounts" ON "venue_accounts"."venue_id" = "venues"."uuid" WHERE "employments"."role" = 1 AND (CONCAT(employments.first_name, ' ', employments.last_name) LIKE '%%') ORDER BY feedbacks.updated_at DESC NULLS LAST LIMIT 25 OFFSET 50

P.S。我使用了will_paginate gem,在这种情况下不显示分页div,尽管如此 @ staff.total_pages == 11

0 个答案:

没有答案