我在我的Rails 4项目中使用了will_paginate gem。当我查看从调用paginate()
返回的对象的祖先时,我想我会在祖先列表中看到与will_paginate相关的内容,例如,可能是WillPaginate::Collection
,但我没有看到类似的内容。如果will_paginate不出现在血统树的任何位置,我很困惑我的对象如何获得像current_page
和total_pages
这样的方法。Rails 4将分页祖先
例如:
location = Location.first
reviews = location.location_reviews.paginate(page: 1, per_page: 5)
reviews.next_page # => 2
reviews.total_pages # => 16
reviews.total_entires # => 80
然而,reviews.ancestors
输出为:
[LocationReview(id: integer, title: string), LocationReview::GeneratedAssociationMethods, #<#<Class:0x007fd6623bec38>:0x007fd6623beda0>, ActiveRecord::Base, GlobalID::Identification, ActiveRecord::Store, ActiveRecord::Serialization, ActiveModel::Serializers::Xml, ActiveModel::Serializers::JSON, ActiveModel::Serialization, ActiveRecord::Reflection, ActiveRecord::NoTouching, ActiveRecord::Transactions, ActiveRecord::Aggregations, ActiveRecord::NestedAttributes, ActiveRecord::AutosaveAssociation, ActiveModel::SecurePassword, ActiveRecord::Associations, ActiveRecord::Timestamp, ActiveModel::Validations::Callbacks, ActiveRecord::Callbacks, ActiveRecord::AttributeMethods::Serialization, ActiveRecord::AttributeMethods::Dirty, ActiveModel::Dirty, ActiveRecord::AttributeMethods::TimeZoneConversion, ActiveRecord::AttributeMethods::PrimaryKey, ActiveRecord::AttributeMethods::Query, ActiveRecord::AttributeMethods::BeforeTypeCast, ActiveRecord::AttributeMethods::Write, ActiveRecord::AttributeMethods::Read, ActiveRecord::Base::GeneratedAssociationMethods, #<#<Class:0x007fd661d14e28>:0x007fd661d14ef0>, ActiveRecord::AttributeMethods, ActiveModel::AttributeMethods, ActiveRecord::Locking::Pessimistic, ActiveRecord::Locking::Optimistic, ActiveRecord::AttributeDecorators, ActiveRecord::Attributes, ActiveRecord::CounterCache, ActiveRecord::Validations, ActiveModel::Validations::HelperMethods, ActiveSupport::Callbacks, ActiveModel::Validations, ActiveRecord::Integration, ActiveModel::Conversion, ActiveRecord::AttributeAssignment, ActiveModel::ForbiddenAttributesProtection, ActiveRecord::Sanitization, ActiveRecord::Scoping::Named, ActiveRecord::Scoping::Default, ActiveRecord::Scoping, ActiveRecord::Inheritance, ActiveRecord::ModelSchema, ActiveRecord::ReadonlyAttributes, ActiveRecord::Persistence, ActiveRecord::Core, Object, ActiveSupport::Dependencies::Loadable, PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]
为什么没有一类相关的这个输出will_paginate?
但'reviews.class'返回'LocationReview :: ActiveRecord_Associations_CollectionProxy'。它不应该返回'WillPaginate :: Collection'吗? – flyingL123
如果你看到这行代码:https://github.com/mislav/will_paginate/blob/74a6cd0197072903cd8b83744133744ff4b4c046/lib/will_paginate/array.rb#L16 当它调用'arr.paginate'它应用后返回一个数组分页选项例如per_page,page等。因此,就你而言,它应该返回一个类“location.location_reviews”的对象,这就是你所得到的,并且是正常的行为。 –
这一行实际上证实,它返回原始集合:https://github.com/mislav/will_paginate/blob/74a6cd0197072903cd8b83744133744ff4b4c046/lib/will_paginate/array.rb#L30但是,不是'WillPaginate :: Collection' –