# Named Scope Woes

**URL:** <https://www.ruby-forum.com/t/named-scope-woes/140754>\
**Category:** Rails\
**Created:** [June 18, 2008, 9:17am UTC](https://www.ruby-forum.com/t/named-scope-woes/140754 "2008-06-18T09:17:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [June 18, 2008, 9:17am UTC](https://www.ruby-forum.com/t/named-scope-woes/140754/1 "2008-06-18T09:17:02Z")

</div>

I’m trying to dynamically build up a complex search screen using named  
scopes very much like in ryans recent railscast at  
[#112 Anonymous Scopes - RailsCasts](http://railscasts.com/episodes/112).

I’m finding issue though that the second named scope is wiping out the  
joins in the first scope.

For Example:  
scope = ProjectTotalView.scoped({})

scope = scope.scoped({  
:joins =\> “INNER JOIN proj\_funding\_schemes\_vw ON  
(proj\_funding\_schemes\_vw.fin\_project\_id =  
project\_totals\_vw.fin\_project\_id)”,  
:conditions =\> “proj\_funding\_schemes\_vw.currency\_code in  
(‘USD’,‘AUD’)”  
})

scope = scope.scoped({  
:joins =\> “INNER JOIN project\_rfcds\_vw ON  
(project\_rfcds\_vw.fin\_project\_id = project\_totals\_vw.fin\_project\_id)”,  
:conditions =\> “project\_rfcds\_vw.rfcd\_code in (‘250400’)”  
})

scope.all

If you swap the scopes around, it’s always the other one that says  
unknown column rfcd\_code etc. It’s because it only does the first join  
that this error occurs.

Any idea on how I can fix (if it is an issue) named\_scope so it does  
combine the :joins instead. I’ve tried looking though  
/active\_record/named\_scope.rb and I can’t actually work out at the point  
where those conditions get added to the “sql builder”

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [June 19, 2008, 3:09am UTC](https://www.ruby-forum.com/t/named-scope-woes/140754/2 "2008-06-19T03:09:46Z")

</div>

I’ve also found this is talked about here:  
[http://www.ruby-forum.com/topic/154275](http://www.ruby-forum.com/topic/154275)

A ticket marked as incomplete on lighthouse:  
[http://rails.lighthouseapp.com/projects/8994/tickets/46-named\_scope-should-support-joins](http://rails.lighthouseapp.com/projects/8994/tickets/46-named_scope-should-support-joins)

A simple fix for:  
[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)

Alex M. wrote:

> I’m trying to dynamically build up a complex search screen using named  
> scopes very much like in ryans recent railscast at  
> [#112 Anonymous Scopes - RailsCasts](http://railscasts.com/episodes/112).
> 
> I’m finding issue though that the second named scope is wiping out the  
> joins in the first scope.
> 
> For Example:  
> scope = ProjectTotalView.scoped({})
> 
> scope = scope.scoped({  
> :joins =\> “INNER JOIN proj\_funding\_schemes\_vw ON  
> (proj\_funding\_schemes\_vw.fin\_project\_id =  
> project\_totals\_vw.fin\_project\_id)”,  
> :conditions =\> “proj\_funding\_schemes\_vw.currency\_code in  
> (‘USD’,‘AUD’)”  
> })
> 
> scope = scope.scoped({  
> :joins =\> “INNER JOIN project\_rfcds\_vw ON  
> (project\_rfcds\_vw.fin\_project\_id = project\_totals\_vw.fin\_project\_id)”,  
> :conditions =\> “project\_rfcds\_vw.rfcd\_code in (‘250400’)”  
> })
> 
> scope.all
> 
> If you swap the scopes around, it’s always the other one that says  
> unknown column rfcd\_code etc. It’s because it only does the first join  
> that this error occurs.
> 
> Any idea on how I can fix (if it is an issue) named\_scope so it does  
> combine the :joins instead. I’ve tried looking though  
> /active\_record/named\_scope.rb and I can’t actually work out at the point  
> where those conditions get added to the “sql builder”
