# Nested with\_scope and :joins

**URL:** <https://discuss.rubyonrails.org/t/nested-with-scope-and-joins/9278>\
**Category:** rubyonrails-core\
**Created:** [February 25, 2007, 12:23am UTC](https://discuss.rubyonrails.org/t/nested-with-scope-and-joins/9278 "2007-02-25T00:23:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Bassett](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@Josh\_Bassett](https://discuss.rubyonrails.org/u/Josh_Bassett)\
**Post date:** [February 25, 2007, 12:23am UTC](https://discuss.rubyonrails.org/t/nested-with-scope-and-joins/9278/1 "2007-02-25T00:23:58Z")

</div>

Hi,

I've been trying to DRY up my models in my current Rails project by adding a dash of the very useful with\_scope. However, I quickly realized that when nesting with\_scopes the outermost :joins are forgotten. This somewhat limits the extent to which I can exploit the use of with\_scope.

Is there any way to preserve :joins through a nested with\_scope stack?

Also, I've noticed that when extending a has\_many :through association with a find(:all, :joins =\> ...) the :through SQL join is forgotten and causes a SQL syntax error. I think that this may be related to the with\_scope :joins problem.

Perhaps someone can shed some light on the reasoning behind this situation.

Thanks,

Josh.

---

<div class="post-metadata">

**Author:** ![bitsweat](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/bitsweat/32/591_2.png) [@bitsweat](https://discuss.rubyonrails.org/u/bitsweat)\
**Post date:** [February 25, 2007, 2:20am UTC](https://discuss.rubyonrails.org/t/nested-with-scope-and-joins/9278/2 "2007-02-25T02:20:13Z")

</div>

Danger, danger! with\_scope considered harmful. with\_scope is not a generic, do-it-all SQL combiner. It was only implemented to allow chaining foreign keys conditions between associations:

invoice.lineitems.find(1) == Lineitem.with\_scope(:find =\> { :conditions =\> [‘invoice\_id = ?’, invoice\_id] }) { Lineitem.find(1) }

Please combine your conditions and joins explicitly if possible.

jeremy
