# Why not merge :joins in with\_scope?

**URL:** <https://www.ruby-forum.com/t/why-not-merge-joins-in-with-scope/138681>\
**Category:** Rails\
**Created:** [May 26, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/why-not-merge-joins-in-with-scope/138681 "2008-05-26T17:25:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![macournoyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@macournoyer](https://www.ruby-forum.com/u/macournoyer)\
**Post date:** [May 26, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/why-not-merge-joins-in-with-scope/138681/1 "2008-05-26T17:25:21Z")

</div>

Hi,

We’re chaining several named\_scope calls like this:

Post.something\_with\_a\_join.another\_with\_a\_join

And we found out the :joins in the scope aren’t merged.

I’m wondering if there’s any reason why :joins aren’t merged  
but :conditions and :include are, see:

> <https://github.com/rails/rails/blob/main/activerecord/lib/active_record/base.rb#L1823>

I know some join could be contradictory but they can also be required  
by the conditions.

I have a patch ready for this but I’m waiting for some feedback before  
writing some tests and submitting it, maybe I’m missing something.

Thanks,  
Marc

---

<div class="post-metadata">

**Author:** ![macournoyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@macournoyer](https://www.ruby-forum.com/u/macournoyer)\
**Post date:** [May 26, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/why-not-merge-joins-in-with-scope/138681/2 "2008-05-26T17:30:06Z")

</div>

On 26 May 2008, at 16:24, macournoyer wrote:

> but :conditions and :include are, see:  
> [http://github.com/rails/rails/tree/master/activerecord/lib/active\_record/base.rb#L1823](http://github.com/rails/rails/tree/master/activerecord/lib/active_record/base.rb#L1823)

:joins comes in 2 forms. When :joins is like :include (ie an array of  
(possibly nested) association names, i don’t see why it couldn’t  
behave like :include. That usage is newish (last autumn?) so it could  
jus be an omission. In the other usage where :joins is an sql fragment  
then it’s rather more problematic (for doing things like removing  
duplicates etc…)

Fred

---

<div class="post-metadata">

**Author:** ![macournoyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@macournoyer](https://www.ruby-forum.com/u/macournoyer)\
**Post date:** [May 26, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/why-not-merge-joins-in-with-scope/138681/3 "2008-05-26T17:38:22Z")

</div>

You’re right, but by not merging :joins it fails in both cases: when  
it’s like include and as an sql fragment

If we merge the :joins like described here:  
[http://blog.teksol.info/2008/5/26/why-are-activerecord-scopes-not-merged](http://blog.teksol.info/2008/5/26/why-are-activerecord-scopes-not-merged)  
, it will only fail in the later case, when it’s used a an sql  
fragment and both conflict.

On May 26, 11:29 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![macournoyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@macournoyer](https://www.ruby-forum.com/u/macournoyer)\
**Post date:** [May 26, 2008, 5:41pm UTC](https://www.ruby-forum.com/t/why-not-merge-joins-in-with-scope/138681/4 "2008-05-26T17:41:55Z")

</div>

On 26 May 2008, at 16:34, francois.beausoleil wrote:

> Shouldn’t the DB server intelligent enough to filter out duplicates in  
> that case ?

Doesn’t sound like the sort of thing you could rely on across the board.

---

<div class="post-metadata">

**Author:** ![macournoyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@macournoyer](https://www.ruby-forum.com/u/macournoyer)\
**Post date:** [May 26, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/why-not-merge-joins-in-with-scope/138681/5 "2008-05-26T17:39:33Z")

</div>

On 26 mai, 11:29, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> :joins comes in 2 forms. When :joins is like :include (ie an array of  
> (possibly nested) association names, i don’t see why it couldn’t  
> behave like :include. That usage is newish (last autumn?) so it could  
> jus be an omission. In the other usage where :joins is an sql fragment  
> then it’s rather more problematic (for doing things like removing  
> duplicates etc…)

Shouldn’t the DB server intelligent enough to filter out duplicates in  
that case ?

Thanks!  
François

---

<div class="post-metadata">

**Author:** ![macournoyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@macournoyer](https://www.ruby-forum.com/u/macournoyer)\
**Post date:** [May 26, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/why-not-merge-joins-in-with-scope/138681/6 "2008-05-26T17:44:52Z")

</div>

On 26 May 2008, at 16:37, macournoyer wrote:

> You’re right, but by not merging :joins it fails in both cases: when  
> it’s like include and as an sql fragment
> 
> If we merge the :joins like described here:  
> [http://blog.teksol.info/2008/5/26/why-are-activerecord-scopes-not-merged](http://blog.teksol.info/2008/5/26/why-are-activerecord-scopes-not-merged)  
> , it will only fail in the later case, when it’s used a an sql  
> fragment and both conflict.

I think you’d probably want to base yourself on what :include does (in  
fact you could probably reuse the merge\_includes code), as you need to  
handle the fact that you can say :joins =\> :foo or :joins =\> [:foo]  
and things like that

Fred

---

<div class="post-metadata">

**Author:** ![macournoyer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@macournoyer](https://www.ruby-forum.com/u/macournoyer)\
**Post date:** [May 26, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/why-not-merge-joins-in-with-scope/138681/7 "2008-05-26T19:24:03Z")

</div>

Hey,

I’m probably the last person to touch the :joins =\> :association style  
syntax so I’d like to chime in with a warning here. The change  
proposed in:

[http://blog.teksol.info/2008/5/26/why-are-activerecord-scopes-not-merged](http://blog.teksol.info/2008/5/26/why-are-activerecord-scopes-not-merged)

sets off alarm bells for me. See the following line:

> <https://github.com/rails/rails/blob/main/activerecord/lib/active_record/base.rb#L1552>

What that line does is ensure that your joins {:like =\> :this}, :or,  
[:like, :this] will be treated as an _inner_ join. If you just  
concatenate the :joins together into a string you’ll break those  
semantics.

I know Fred has already brought it to your attention that you should  
base your code off the way that :includes are handled (merge\_includes)  
but I just wanted to add some weight to that.

And as for “why doesn’t it already do what it ought to?”… I’m pretty  
sure that when I reworked that stuff I didn’t actually _remove_  
anything so I kind of hope it never merged joins in scopes in the  
first place… if I did remove something critical then I shall quietly  
flog myself 😛

Regards,  
Trevor

On 5/26/08, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > , it will only fail in the later case, when it’s used a an sql
> > 
> > > (possibly nested) association names, i don’t see why it couldn’t
> > > 
> > > > by the conditions.

## –

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)
