Will_paginate 和 DataMapper 仅在 irb 中工作

发布于 2024-12-15 04:33:47 字数 3130 浏览 3 评论 0原文

我正在尝试让 will_paginate 与 Sinatra 一起工作,但 will_paginate 和 DataMapper 遇到了一个奇怪的问题。所以我有一个代码:

require 'data_mapper'
require 'will_paginate'
require 'will_paginate/data_mapper'
class User
  include DataMapper::Resource

  property :id,   Serial    # primary serial key
  property :username, String, length: 5..15, unique: true
  property :fullname, String, length: 5..25
  property :email, String, required: true, unique: true, format: :email_address
  property :hashed_password, String
  property :created_at, Time, required: true
  property :auth_token, String
  property :locale, String

  has n, :items
end

class Item
  include DataMapper::Resource
  property :id, Serial
  property :question, String, required: true
  property :answer, String, required: true
  property :ef, Float
  property :interval, Float
  property :created_at, Time
  property :updated_at, Time
  property :reviev_at, Time

  belongs_to :user
end

DataMapper::setup(:default, "sqlite3://#{File.expand_path(File.dirname(__FILE__))}/database2.db")
DataMapper.finalize
DataMapper.auto_upgrade!

u = User.first
p = u.items.paginate(page: 1)
puts p.total_entries

运行它会导致一个问题:

/home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:942:in `assert_valid_order': +options[:order]+ should not be empty if +options[:fields] contains a non-operator (ArgumentError)
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:773:in `block in assert_valid_options'
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:766:in `each'
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:766:in `assert_valid_options'
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:363:in `update'
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:386:in `merge'
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/will_paginate-3.0.2/lib/will_paginate/data_mapper.rb:54:in `total_entries'
    from test.rb:39:in `<main>'

但是如果我注释掉最后三行,它在 irb 中可以正常工作:

ruby-1.9.2-p290 :001 > require './test' # file with deleted last 3 lines
 => true 
ruby-1.9.2-p290 :002 > u = User.first
 => #<User @id=1 @username="Testt" @fullname="tetestse" @email="[email protected]" @hashed_password=nil @created_at=2011-11-14 00:00:00 +0100 @auth_token=nil @locale=nil> 
ruby-1.9.2-p290 :003 > p = u.items.paginate(page: 1)
 => [#<Item @id=1 @question="A" @answer="B" @ef=nil @interval=nil @created_at=2011-11-14 00:00:00 +0100 @updated_at=2011-11-14 00:00:00 +0100 @reviev_at=nil @user_id=1>, #<Item @id=2 @question="C" @answer="D" @ef=nil @interval=nil @created_at=2011-11-14 00:00:00 +0100 @updated_at=2011-11-14 00:00:00 +0100 @reviev_at=nil @user_id=1>] 
ruby-1.9.2-p290 :004 > puts p.total_entries 
2
 => nil 

I'm trying to get will_paginate working with Sinatra and I have a stange problem with will_paginate and DataMapper. So I have a code:

require 'data_mapper'
require 'will_paginate'
require 'will_paginate/data_mapper'
class User
  include DataMapper::Resource

  property :id,   Serial    # primary serial key
  property :username, String, length: 5..15, unique: true
  property :fullname, String, length: 5..25
  property :email, String, required: true, unique: true, format: :email_address
  property :hashed_password, String
  property :created_at, Time, required: true
  property :auth_token, String
  property :locale, String

  has n, :items
end

class Item
  include DataMapper::Resource
  property :id, Serial
  property :question, String, required: true
  property :answer, String, required: true
  property :ef, Float
  property :interval, Float
  property :created_at, Time
  property :updated_at, Time
  property :reviev_at, Time

  belongs_to :user
end

DataMapper::setup(:default, "sqlite3://#{File.expand_path(File.dirname(__FILE__))}/database2.db")
DataMapper.finalize
DataMapper.auto_upgrade!

u = User.first
p = u.items.paginate(page: 1)
puts p.total_entries

And running it causes a problem:

/home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:942:in `assert_valid_order': +options[:order]+ should not be empty if +options[:fields] contains a non-operator (ArgumentError)
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:773:in `block in assert_valid_options'
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:766:in `each'
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:766:in `assert_valid_options'
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:363:in `update'
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/query.rb:386:in `merge'
    from /home/changs/src/PwS/vendor/bundle/ruby/1.9.1/gems/will_paginate-3.0.2/lib/will_paginate/data_mapper.rb:54:in `total_entries'
    from test.rb:39:in `<main>'

But if I comment out last three lines it works in irb as fallow:

ruby-1.9.2-p290 :001 > require './test' # file with deleted last 3 lines
 => true 
ruby-1.9.2-p290 :002 > u = User.first
 => #<User @id=1 @username="Testt" @fullname="tetestse" @email="[email protected]" @hashed_password=nil @created_at=2011-11-14 00:00:00 +0100 @auth_token=nil @locale=nil> 
ruby-1.9.2-p290 :003 > p = u.items.paginate(page: 1)
 => [#<Item @id=1 @question="A" @answer="B" @ef=nil @interval=nil @created_at=2011-11-14 00:00:00 +0100 @updated_at=2011-11-14 00:00:00 +0100 @reviev_at=nil @user_id=1>, #<Item @id=2 @question="C" @answer="D" @ef=nil @interval=nil @created_at=2011-11-14 00:00:00 +0100 @updated_at=2011-11-14 00:00:00 +0100 @reviev_at=nil @user_id=1>] 
ruby-1.9.2-p290 :004 > puts p.total_entries 
2
 => nil 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2024-12-22 04:33:50

发生这种情况是因为从脚本调用 p.total_entries 时未加载 p。在 irb 中,设置 p 后打印的值会导致其被加载。您可以通过在调用 total_entries 之前重新加载来解决这个问题,如下所示:

u = User.first
p = u.items.paginate(page: 1)
p.reload
puts p.total_entries

对我来说,这似乎是 will_paginate 中的一个错误,应该报告。

This happens because p isn't loaded when p.total_entries is called from the script. In irb printing the value after setting p causes it to be loaded. You can work around it by reloading before calling total_entries, like this:

u = User.first
p = u.items.paginate(page: 1)
p.reload
puts p.total_entries

To me this seems like a bug in will_paginate that should be reported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文