rails和kaminari分页,每页具有一定的优先级

6yoyoihd  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(358)

我有一个复杂的查询,一部分作为mysql数据库视图编写,另一部分作为rails中的activerecord逻辑编写。每个记录都有自己的优先级,从0到4,其中4是最高优先级。
我正在使用kaminari进行分页,我想知道是否有一种方法可以用一些额外的规则显示每页记录集:
在第一页显示所有#4个优先级行
取每页的页码并用以下公式显示优先级3:0.3*每页
然后对优先级2执行相同的操作
如果这三个步骤都没有产生100%的每页,则显示优先级为0和1的其余步骤
如何使用rails来获得结果。还是直接在sql中实现更好?
以下是我的db视图示例:

select *
from (
        select 
            s.id as source_id,
            'Spree::Store' as source_type, 
            (case when (s.created_at >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND s.created_at < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY)
            then
                'new'
            else
                'old'
            end) as sub_type, 
            1 as priority, 
            s.created_at as created_at, 
            s.updated_at as updated_at, 
            null as owner_id
        from spree_stores as s
        where s.image_id is not NULL and s.is_hidden = false

    union 

    select 
        e.id as source_id, 
        'Event' as source_type, 
        (case 
        when (e.status = 1 and e.is_featured is false)
        then
            'live'
        when (e.is_featured = true)
        then
            'featured'
        else
            case when (e.created_at >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND e.created_at < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY)
            then
                'new'
            else
                'old'
            end
        end) as sub_type, 
        (case 
        when (e.status = 1 or e.is_featured is true)
        then
            3
        else
            1
        end) as priority, 
        e.created_at as created_at, 
        e.updated_at as updated_at, 
        null as owner_id
    from events as e
    where e.status >= 1 and e.expires_at >= curdate()

    union 
    select 
        o.id as source_id, 
        'Spree::Order' as source_type, 
        (case when (o.created_at >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND o.created_at < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY)
        then
            'new'
        else
            'old'
        end) as sub_type,
        1 as priority, 
        o.created_at as created_at, 
        o.updated_at as updated_at, 
        o.user_id as owner_id
    from spree_orders as o
    where o.user_id is not NULL and o.share is true and o.state = 'complete' and o.completed_at is not NULL
    union
    select 
        p.id as source_id, 
        'Spree::Product' as source_type, 
        (case when (p.created_at >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND p.created_at < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY)
        then
            'new'
        else
            'old'
        end) as sub_type, 
        1 as priority, 
        p.created_at as created_at, 
        p.updated_at as updated_at, 
        null as owner_id
    from spree_products as p
    join spree_variants as sv on (sv.product_id = p.id and sv.is_master = true)
    join spree_assets as sa on (sa.viewable_id = sv.id and sa.viewable_type = 'Spree::Variant')
    where p.deleted_at is NULL
    group by p.id
  ) a
  order by priority desc, created_at desc;

这是我得到的结果(只有几行不是全部200个结果):

j9per5c4

j9per5c41#

这听起来比kaminari更复杂的逻辑,也许值得你自己去做。kaminari当然可以方便地创建一个快速分页ui,但是与滚动您自己的解决方案相比,它并没有增加太多的价值。你也许可以根据自己的需要修改它,但这可能比自己动手更让人头疼。
我也有点怀疑你想要的复杂算法是否真的能让用户受益。只有您知道这一点,但您可能需要考虑一个简单的“score”或“rank”列,然后使用kaminari和按score desc排序的查询。

相关问题