Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

undefined method `paginate' for []:DataMapper::Collection #218

tmishra opened this Issue Jan 9, 2012 · 8 comments


None yet
4 participants

tmishra commented Jan 9, 2012


I am trying to use will_paginate as a gem in my Rails app. I am
using Rails 3.0.5 and Ruby 1.8.7.

Here is my issue:


# Pagination related
#gem 'will_paginate', '~> 3.0.0'
gem 'will_paginate', '~> 3.0.pre'

Controller action:

@foo = Foo.all(:order => [:created_at.desc]).paginate(:page => params[:page], :per_page => 1)

View code:

<%= will_paginate @foo %>

Now; trying it in my browser.

Result: [ERROR]

undefined method `paginate' for []:DataMapper::Collection

Can anyone give me a hand to sort this out or a pointer as to where I can look.

Followed : https://github.com/mislav/will_paginate/wiki/Troubleshooting for Datamapper but not much achieved: There is no test for datamapper.

Am I missing something obvious?

Thanks heaps in Advance,



mislav commented Jan 10, 2012

You should specify the version like this to ensure you get the latest version:

gem 'will_paginate', '~> 3.0.2'

DataMapper integration isn't configured to automatically load. You can try putting this in the end of your "config/application.rb":

require "will_paginate/data_mapper"

tmishra commented Jan 10, 2012

Thanks a lot mislav;

This will get my UI working; but for some reason the cucumber test for my page throws:

invalid page: 0 (RangeError)

line 53 is

foo_bar = { Foo_Bar.first || Foo_Bar.make }

Any ideas why (Please)? Does explicit require "will_paginate/datamapper" does something to already exisiting datamapper which is highly unlikely.

dm-core version 1.0.2.



mislav commented Jan 10, 2012

I don't know why you are getting the invalid page error. Must be something in your code. I can't know without full code and stack trace. Try to reproduce this in a minimal script then paste it here, stating of course versions of related libraries such as RSpec, Machinist and so on.

tmishra commented Jan 11, 2012


Thanks a lot for your replies; I tried to reproduce this in a minimal script. (I cannot use the same example because I will need proper permissions from managers to do that)

Solution: (Can you please confirm if this hack is fine or will fail)

I was able to resolve id by replacing: require 'will_paginate/data_mapper' to require 'will_paginate/array'

I was thinking that inside https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/data_mapper.rb

line 63 to 68; the to_a is the cause? (replaces the existing to_a)

Code to reproduce the issue

Again; before the elaboration: if in -test.rb code (mentioned below), I replace line 5: require 'will_paginate/data_mapper' to require 'will_paginate/array' then the test will pass.

I also found the interesting issue #197 which is I think exactly the cause of test failing in this case & the example below I have re-used the code.

## Gemfile
source :rubygems

dm_version = "1.0.2"

gem 'dm-core',           '1.0.2'
gem 'dm-types',          dm_version
gem 'dm-validations',    dm_version
gem 'dm-aggregates',     dm_version
gem 'dm-observer',       dm_version
gem 'dm-pager',          '~> 1.1'
gem 'dm-transactions',   dm_version
gem 'dm-nested-transactions', '0.0.4'
gem 'dm-oracle-adapter', dm_version
gem 'dm-postgres-adapter'
gem 'dm-timestamps',     dm_version
gem 'will_paginate', '3.0.2'
gem 'dm-sqlite-adapter'
gem 'dm-migrations'
gem 'rspec'

## test.rb

require 'dm-core'
require 'dm-migrations'

require 'will_paginate'
require 'will_paginate/data_mapper'

class Test_Event_Handler
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite::memory:')

class Account
  include DataMapper::Resource

  property :id, Serial
  property :name, String

  ##### One-to-many-through
  has n, :authorships
  has n, :events, :through => :authorships


class Event
  include DataMapper::Resource

  property :id, Serial
  property :name, String

  ##### One-to-many-through
  has n, :authorships
  has 1, :owner, :model => 'Account', :through => :authorships, :via => :account


##### One-to-many-through
class Authorship
  include DataMapper::Resource

  belongs_to :account, :key => true
  belongs_to :event, :key => true


  def create_new_event
    puts "Creating new user"
    account = Account.create(:name => 'test user')
    if account.save
      puts "successfully created user '#{account.name}'"

      puts 'Creating new event'
      event = Event.create(:name => 'test event')
      if event.save
        puts "#{event.name}"
        event.errors.each{ |e| puts e }

      puts 'Adding account to event'
      event.owner = account
      if event.save
        puts "#{event.owner.name}"
        event.errors.each{ |e| puts e }


## test_spec.rb

require 'test'

describe Test_Event_Handler, "#create_new_event" do
  it "returns nothing" do
    test = Test_Event_Handler.new
    20.times do 
$ bundle install

$ bundle exec rspec test_spec.rb

       invalid page: 0

Now; if you will change line 5 in test.rb from: require 'will_paginate/datamapper' to: require 'will_paginate/array'

$ bundle exec rspec test_spec.rb

BINGO - works now!

GBH commented Jan 30, 2012

I'm getting same error when specifying will_paginate in the engine Gemfile and then trying to use that engine in the app.
It has nothing to do with datamapper though, just your regular ActiveRecord.
I'll dig a bit more to find out why exactly it does that.

tmishra commented Jan 30, 2012


Cool, if you try the example and solution mentioned above I was getting this in a specific case of including will_paginate/datamapper (But yeah its not a DM issue); Can you try this please:


Please try changing the scope of "def to_a" to private. (I will give it a go as well).

Thanks for replying,


mislav added a commit that referenced this issue Jan 31, 2012


mislav commented Jan 31, 2012

Thanks for all the info, guys. Try out the just released 3.0.3 version

@mislav mislav closed this Jan 31, 2012

I found that when Im adding those line into my application.rb or another line (from another source) into my enviroment.rb just make my application error. But I just add to gemfile n run bundle install. N restart my server. Just simply done.

Good job back there for the paginate :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment