rails-sqlserver/activerecord-sqlserver-adapter — Issue #167: Problems with ActiveRecord models that are paginated
Author: metalbot
Created: 2012-02-23 01:26:22+00:00
State: open

I've been using the sql server adapter to connect a rails front end to a legacy database. It's been working great for everything that I need it to do.

However, I started playing with the activeadmin gem yesterday and started to see an odd error:

TinyTds::Error: No column name was specified for column 2 of '__rnt'.: EXEC
sp_executesql N'SELECT TOP (1) [__rnt].* FROM ( SELECT ROW_NUMBER() OVER (ORDER
BY [Payments].[UPaymentID] ASC) AS [__rn], 1 FROM [Payments] ) AS [__rnt]
WHERE [__rnt].[__rn] > (0) ORDER BY [__rnt].[__rn] ASC'

I think I've narrowed down what the problem is. ActiveAdmin uses the kaminari gem for pagination. For the most part this works fine, until we call the exists? method of an ActiveRecord::Relation that has the kaminari pagination applied.

If you want to reproduce this, add kaminari to a rails project. This will automatically support pagination. From the rails console, grab an ActiveRecord:: Relation from your model , for example:

p = Payment.limit(10).order("payment_date") # gives us an ActiveRecord::Relation
p.exists? #works fine here
p = Payment.page(1).per(5) # gives us another ActiveRecord::Relation
p.exists? # blows up here with the TinyTDS error above