Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
tmishra opened this Issue · 8 comments

4 participants

@tmishra

Hiya,

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:

Gemfile:

# 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,

-Tats

@mislav
Owner

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

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)
./spec/blueprints.rb:53

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.

-Tats

@mislav
Owner

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

Hiya,

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
  ###

end

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
  ###

end

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

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

  DataMapper.finalize
  DataMapper.auto_upgrade!

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


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

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

end

## 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 
       test.create_new_event()
    end 
  end
end
$ bundle install

$ bundle exec rspec test_spec.rb

     RangeError:
       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

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

Hiya,

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:

https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/data_mapper.rb

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

Thanks for replying,

Cheers,
-Tats

@mislav
Owner

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

@mislav mislav closed this
@ksugiarto

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
Something went wrong with that request. Please try again.