Problem with paginate on sql server 2005

7 views
Skip to first unread message

Accessd

unread,
Mar 31, 2011, 3:05:36 AM3/31/11
to Rails SQLServer Adapter
In the project I use MS SQL Server 2005 with will_paginate
Pagination works well if I do inquiry:
Tariffication.paginate(:page => 1, :per_page => 30)
But doesn't work on inquiry:
Tariffication.includes(:otdel).paginate(:page => 1, :per_page => 30)

With an error: Ambiguous column name 'ID'.
Thus generated inquiry (in a short form) following:
SELECT TOP (30) t0_r0, t1_r2 FROM ( SELECT ROW_NUMBER() OVER (ORDER BY
ID ASC) AS [rn], [t_main].[ID] AS t0_r0, [t_Otdel].[Name] AS t1_r2
FROM [t_main] LEFT OUTER JOIN [t_Otdel] ON [t_Otdel].[ID] = [t_main].
[ID_t_Otdel] ) AS [rnt] WHERE [rnt].[rn] > (0)

It doesn't help: Tariffication.includes(:otdel).paginate(:page =>
1, :per_page => 30, :order => "[t_main].[ID]")

Ken Collins

unread,
Mar 31, 2011, 7:31:33 AM3/31/11
to rails-sqlse...@googlegroups.com

Other databases allow you to not specify an order during window functions (limit/offset). SQL Server is not one of those databases and the adapter/visitor has always tried to shim in a primary key as best it can when that is left off. This means that you must be explicit where this breaks down. In your case if you specify an order that'll generated the more qualified table.column, you will be OK.

- Ken

Reply all
Reply to author
Forward
0 new messages