Rails 5 - 如何使用 will_paginate 和活动模型序列化程序获取总条目
Rails 5 - How to get total entries using will_paginate and Active Model Serializers
我正在使用 will_paginate
和 api-pagination
对我的数据进行分页,active_model_serializers
用于 JSON 序列化。
我想将资源的总条目发送到我正在使用 AngularJS.
的客户端
控制器代码
def index
comp_id = params[:company_id]
cat_id = params[:category_id]
if comp_id
if comp_id && cat_id
product = Product.where(:company_id => comp_id, :category_id => cat_id)
else
product = Product.where(:company_id => comp_id)
end
paginate json: product,meta: pagination_dict(product), status: 200
else
render json: { errors: "Company ID is NULL" }, status: 422
end
end
序列化器
class ProductSerializer < ActiveModel::Serializer
attributes :id, :name, :mrp, :sp, :cp, :stocks, :isPublished
has_one :category
end
如何在我的回复中包含 total_count?
更新
我尝试按照 this 文档在我的基本控制器中添加以下方法,但是对于所有已定义的方法,我都遇到了未定义的方法错误。
class ApplicationController < ActionController::Base
include Authenticable
include Rails::Pagination
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
protect_from_forgery with: :null_session
def render_404
render json: { errors: 'The requested resource cannot be found' }, status: 404
end
def pagination_dict(collection)
{
#current_page: collection.current_page,
#next_page: collection.next_page,
#prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
total_pages: collection.total_pages,
total_count: collection.total_count
}
end
end
这些方法(total_pages
、total_entries
...)仅适用于 paginated
ActiveRecord 关系。
例如Product.where(company_id: comp_id).page(1).total_pages
returns总页数,因为关系使用pagination
.
对于您的情况,为什么不直接使用 ActiveRecord 的 count
方法来获取条目总数?
paginate json: product, meta: product.count, status: 200
我正在使用 will_paginate
和 api-pagination
对我的数据进行分页,active_model_serializers
用于 JSON 序列化。
我想将资源的总条目发送到我正在使用 AngularJS.
控制器代码
def index
comp_id = params[:company_id]
cat_id = params[:category_id]
if comp_id
if comp_id && cat_id
product = Product.where(:company_id => comp_id, :category_id => cat_id)
else
product = Product.where(:company_id => comp_id)
end
paginate json: product,meta: pagination_dict(product), status: 200
else
render json: { errors: "Company ID is NULL" }, status: 422
end
end
序列化器
class ProductSerializer < ActiveModel::Serializer
attributes :id, :name, :mrp, :sp, :cp, :stocks, :isPublished
has_one :category
end
如何在我的回复中包含 total_count?
更新
我尝试按照 this 文档在我的基本控制器中添加以下方法,但是对于所有已定义的方法,我都遇到了未定义的方法错误。
class ApplicationController < ActionController::Base
include Authenticable
include Rails::Pagination
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
protect_from_forgery with: :null_session
def render_404
render json: { errors: 'The requested resource cannot be found' }, status: 404
end
def pagination_dict(collection)
{
#current_page: collection.current_page,
#next_page: collection.next_page,
#prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
total_pages: collection.total_pages,
total_count: collection.total_count
}
end
end
这些方法(total_pages
、total_entries
...)仅适用于 paginated
ActiveRecord 关系。
例如Product.where(company_id: comp_id).page(1).total_pages
returns总页数,因为关系使用pagination
.
对于您的情况,为什么不直接使用 ActiveRecord 的 count
方法来获取条目总数?
paginate json: product, meta: product.count, status: 200