mislav/will_paginate — Issue #197: will_paginate error on associations-based models in DataMapper
Author: mamantoha
Created: 2011-11-10 13:34:07+00:00
State: open

Very simple example:

```ruby
# -*- encoding: utf-8 -*-
require 'dm-core'
require 'dm-migrations'

require 'will_paginate'
require 'will_paginate/data_mapper'

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 [19/694]
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!

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

Output:

```
~ (0.000298) PRAGMA table_info("accounts")
~ (0.000053) SELECT sqlite_version(*)
~ (0.000677) CREATE TABLE "accounts" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50))
~ (0.000045) PRAGMA table_info("events")
~ (0.000289) CREATE TABLE "events" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50))
~ (0.000050) PRAGMA table_info("authorships")
~ (0.000374) CREATE TABLE "authorships" ("account_id" INTEGER NOT NULL, "event_id" INTEGER NOT NULL, PRIMARY KEY("account_id", "event_id"))
~ (0.000295) CREATE INDEX "index_authorships_account" ON "authorships" ("account_id")
~ (0.000282) CREATE INDEX "index_authorships_event" ON "authorships" ("event_id")
Creating new user
~ (0.000117) INSERT INTO "accounts" ("name") VALUES ('test user')
successfully created user 'test user'

Creating new event
~ (0.000122) INSERT INTO "events" ("name") VALUES ('test event')
test event

Adding account to event
/home/anton/.rvm/gems/ruby-1.9.2-p290/gems/will_paginate-3.0.2/lib/will_paginate/page_number.rb:16:in `Integer': can't convert nil into Integer (TypeError)
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/will_paginate-3.0.2/lib/will_paginate/page_number.rb:16:in `initialize'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/will_paginate-3.0.2/lib/will_paginate/page_number.rb:54:in `new'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/will_paginate-3.0.2/lib/will_paginate/page_number.rb:54:in `PageNumber'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/will_paginate-3.0.2/lib/will_paginate/collection.rb:62:in `initialize'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/will_paginate-3.0.2/lib/will_paginate/collection.rb:95:in `new'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/will_paginate-3.0.2/lib/will_paginate/collection.rb:95:in `create'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/will_paginate-3.0.2/lib/will_paginate/data_mapper.rb:64:in `to_a'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/dm-core-1.2.0/lib/dm-core/collection.rb:269:in `first'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/dm-core-1.2.0/lib/dm-core/associations/one_to_one.rb:19:in `get'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/dm-core-1.2.0/lib/dm-core/resource/persistence_state.rb:18:in `get'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/dm-core-1.2.0/lib/dm-core/resource/persistence_state/persisted.rb:9:in `get'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/dm-core-1.2.0/lib/dm-core/resource/persistence_state/dirty.rb:47:in `track'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/dm-core-1.2.0/lib/dm-core/resource/persistence_state/dirty.rb:8:in `set'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/dm-core-1.2.0/lib/dm-core/resource/persistence_state/clean.rb:15:in `set'
from /home/anton/.rvm/gems/ruby-1.9.2-p290/gems/dm-core-1.2.0/lib/dm-core/model/relationship.rb:360:in `owner='
from dm_bug1.rb:66:in `

'
```

If comment 2 lines with will_paginate. Code is executed successfully.

```ruby
#require 'will_paginate'
#require 'will_paginate/data_mapper'
```