Skip to content

Commit da2131d

Browse files
committed
Unique has_many associations with pagination now work. Fixes #209
1 parent 89f1f58 commit da2131d

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
* 3.2.6 *
3+
4+
* Unique has_many associations with pagination now work. Fixes #209
5+
6+
27
* 3.2.5 *
38

49
* Fix a few test from ActiveRecord 3.2.6 upgrade.

lib/active_record/connection_adapters/sqlserver/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module ConnectionAdapters
33
module Sqlserver
44
module Version
55

6-
VERSION = '3.2.5'
6+
VERSION = '3.2.6'
77

88
end
99
end

lib/arel/visitors/sqlserver.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def visit_Arel_Nodes_SelectStatementWithOutOffset(o, windowed=false)
154154
projections = projections.map { |x| projection_without_expression(x) }
155155
end
156156
[ ("SELECT" if !windowed),
157-
(visit(core.set_quantifier) if core.set_quantifier),
157+
(visit(core.set_quantifier) if core.set_quantifier && !windowed),
158158
(visit(o.limit) if o.limit && !windowed),
159159
(projections.map{ |x| v = visit(x); v == "1" ? "1 AS [__wrp]" : v }.join(', ')),
160160
(source_with_lock_for_select_statement(o)),
@@ -166,13 +166,14 @@ def visit_Arel_Nodes_SelectStatementWithOutOffset(o, windowed=false)
166166
end
167167

168168
def visit_Arel_Nodes_SelectStatementWithOffset(o)
169+
core = o.cores.first
169170
o.limit ||= Arel::Nodes::Limit.new(9223372036854775807)
170171
orders = rowtable_orders(o)
171172
[ "SELECT",
172173
(visit(o.limit) if o.limit && !windowed_single_distinct_select_statement?(o)),
173174
(rowtable_projections(o).map{ |x| visit(x) }.join(', ')),
174175
"FROM (",
175-
"SELECT ROW_NUMBER() OVER (ORDER BY #{orders.map{ |x| visit(x) }.join(', ')}) AS [__rn],",
176+
"SELECT #{core.set_quantifier ? 'DISTINCT DENSE_RANK()' : 'ROW_NUMBER()'} OVER (ORDER BY #{orders.map{ |x| visit(x) }.join(', ')}) AS [__rn],",
176177
visit_Arel_Nodes_SelectStatementWithOutOffset(o,true),
177178
") AS [__rnt]",
178179
(visit(o.offset) if o.offset),

test/cases/offset_and_limit_test_sqlserver.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
require 'cases/sqlserver_helper'
2+
require 'models/job'
3+
require 'models/person'
4+
require 'models/reference'
25
require 'models/book'
6+
require 'models/author'
37
require 'models/post'
8+
require 'models/comment'
9+
require 'models/categorization'
410

511
class OffsetAndLimitTestSqlserver < ActiveRecord::TestCase
612

7-
fixtures :posts
13+
fixtures :jobs, :people, :references,
14+
:authors, :posts, :comments, :categorizations
815

916
setup :create_10_books
1017
teardown :destroy_all_books
@@ -68,6 +75,23 @@ class OffsetAndLimitTestSqlserver < ActiveRecord::TestCase
6875
order_row_numbers = SqlServerOrderRowNumber.offset(7).order("c DESC").select("c, ROW_NUMBER() OVER (ORDER BY c ASC) AS [dummy]").all.map(&:c)
6976
assert_equal [2, 1, 0], order_row_numbers
7077
end
78+
79+
should 'work with through associations' do
80+
assert_equal people(:david), jobs(:unicyclist).people.limit(1).offset(1).all.first
81+
end
82+
83+
should 'work with through uniq associations' do
84+
david = authors(:david)
85+
mary = authors(:mary)
86+
thinking = posts(:thinking)
87+
# Mary has duplicate categorizations to the thinking post.
88+
assert_equal [thinking, thinking], mary.categorized_posts.all
89+
assert_equal [thinking], mary.unique_categorized_posts.limit(2).offset(0)
90+
# Paging thru David's uniq ordered comments.
91+
assert_equal [1, 2, 3, 5, 6, 7, 8, 9, 10, 12], david.ordered_uniq_comments.map(&:id)
92+
assert_equal [3, 5], david.ordered_uniq_comments.limit(2).offset(2).map(&:id)
93+
assert_equal [7, 8, 9, 10, 12], david.ordered_uniq_comments.limit(5).offset(5).map(&:id)
94+
end
7195

7296
context 'with count' do
7397

0 commit comments

Comments
 (0)