taxon = Spree::Taxon.find_by_permalink('categories/outlet')
on_sale_products = taxon.products.on_hand
discontinued_variant_products = load_products_with_discontinued_variants
@PageItemLimit = 4
if params[:page]
offset = (params[:page].to_i) * @PageItemLimit
else
offset = 0
end
products = discontinued_variant_products | on_sale_products
products = products[(offset) , @PageItemLimit]
def load_products_with_discontinued_variants
key = Spree::Product.discontinued_variants_cache_key
product_ids = Rails.cache.fetch key, expires_at: 1.day do
Spree::Product
.with_discontinued_variants
.select { |product| product.variants.any? { |v| v.on_sale? &&
v.in_stock? } }
.pluck(:id)
end
Spree::Product.with_discontinued_variants.where("id IN (?)", product_ids)
end
add_search_scope :with_discontinued_variants do
select(' distinct spree_products.*').includes(variants: { prices: {
sale_prices: :calculator }}).where(discontinued_variants: true)
end
这是我目前的分页设置,
discontinued_variant_products属于活动记录关系对象。 on_sale_products属于活动记录关联类型。
当前偏移量,限制逻辑应用于作为数组的产品,保存活动记录对象联合的结果(所有数据)。 我想在活动记录对象上使用limit和offset关键字进行分页,但因为它们是两个对象,所以在两个对象上都处理limit-offset 单独给出不正确的结果,有些可以为我提供正确的逻辑,或者如果两个活动记录对象可以合并到新的Active记录对象中, 极限偏移可以很容易地应用于它。