# Conditional join selection with active record

**URL:** <https://www.ruby-forum.com/t/conditional-join-selection-with-active-record/150968>\
**Category:** Rails\
**Created:** [October 15, 2008, 11:42am UTC](https://www.ruby-forum.com/t/conditional-join-selection-with-active-record/150968 "2008-10-15T11:42:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![inriz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/50afbb/32.png) [@inriz](https://www.ruby-forum.com/u/inriz)\
**Post date:** [October 15, 2008, 11:42am UTC](https://www.ruby-forum.com/t/conditional-join-selection-with-active-record/150968/1 "2008-10-15T11:42:21Z")

</div>

hello list,

i’m searching for an implementation of the following sql statement:

SELECT inserent.\* FROM inserent  
join  
(select objpriv.maknr from objpriv  
where objpriv.bankkto like ‘593%’ and objpriv.zahlart = ‘B’  
group by objpriv.maknr) o on o.maknr = inserent.insnr

ORDER BY  
inserent.AENDDATUM desc

evidently, i solved my problem with

@advertisers = Advertiser.paginate( :select =\> “inserent.\*”, :order =\>  
“inserent.AENDDATUM desc”, :from =\> “inserent join (select  
objpriv.maknr from objpriv where objpriv.bankkto like ‘#{query}’ and  
objpriv.zahlart = ‘B’ group by objpriv.maknr) o on o.maknr =  
inserent.insnr”)

but, the pagination is broken.  
i know i’m abusing the from parameter for my needs

what’s the way to implement it?

thanks for all your help

best regards

---

<div class="post-metadata">

**Author:** ![inriz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/50afbb/32.png) [@inriz](https://www.ruby-forum.com/u/inriz)\
**Post date:** [October 15, 2008, 12:38pm UTC](https://www.ruby-forum.com/t/conditional-join-selection-with-active-record/150968/2 "2008-10-15T12:38:07Z")

</div>

On 15 Oct 2008, at 10:41, inriz wrote:

> but, the pagination is broken.  
> i know i’m abusing the from parameter for my needs

Use the :joins parameter ?

Fred
