On Thursday, April 16, 2015 at 4:20:28 PM UTC-7, Steven Ringo wrote:
>
> Hi,
>
> I am trying to do pagination on the parent of a model association, yet 
> show all the associated children for each "page" of parents.
>
> Ideally I would like to use a library like will_paginate or kaminari to 
> save myself some effort creating the rendered, paged views.
>
> I have the following models in a Rails app:
>
> class Recipient < Sequel::Model
>   one_to_many :devicesend
> class Device < Sequel::Model
>   many_to_one :recipientsend
>
> i.e. Each Recipient has one or more Devices
>
> To paginate on Recipient but show all Devices for each page of recipients 
> respectively, in SQL something like this works:
>
> SELECT 
>   paged_recipients.first_name, paged_recipients.last_name, devices.*FROM
>   (
>     SELECT first_name, last_name 
>     FROM recipients ORDER by id OFFSET 10 LIMIT 10
>   ) paged_recipientsINNER JOIN
>   devices ON paged_recipients.id = devices.recipient_idORDER BY
>   paged_recipients.id
>
> Any idea if this can this be done in Sequel, ideally using the pagination 
> extension?
>
> Any help would be hugely appreciated.
>
Assuming that doing it in a two queries instead of a single query is 
acceptable:

  Recipient.eager(:devices).paginate(2, 10).all

That will give you Recipient instances with the devices association 
preloaded.

It's certainly possible to do what you want it in a single query:

  Device.join(Recipient.order(:id).paginate(2, 10).as(:paged_recipients), 
:id=>:recipient_id).
    order(:paged_recipients__id).
    select_all(:devices).
    select_append(:paged_recipients__first_name, 
:paged_recipients__last_name).all

That gives you Device instances that also include the first and last names 
of the related recipient.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to