# Chaining two named\_scope that have a block of code

**URL:** <https://www.ruby-forum.com/t/chaining-two-named-scope-that-have-a-block-of-code/149522>\
**Category:** Rails\
**Created:** [September 25, 2008, 7:20pm UTC](https://www.ruby-forum.com/t/chaining-two-named-scope-that-have-a-block-of-code/149522 "2008-09-25T19:20:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [September 25, 2008, 7:20pm UTC](https://www.ruby-forum.com/t/chaining-two-named-scope-that-have-a-block-of-code/149522/1 "2008-09-25T19:20:31Z")

</div>

Hi,

## I have a named\_scope that retrieves all the products for the current website based on its domain name:

## named\_scope :site, lambda { |website\_name| { :joins =\> :site, :order =\> ‘products.created\_at DESC’, :conditions =\> [‘sites.domain\_name = ?’, website\_name]}}

## Then I would like to chain another named\_scope to the previous one, so that it adds one more condition to the query:

named\_scope :single, lambda { |permalink| { :conditions =\> [‘permalink =  
?’, permalink], :limit =\> 1 } }

## Then in my controller I invoke:

@product = Product.site(website\_name).single(params[:permalink])

## And here is the SQL generated:

## SELECT count(\*) AS count\_all FROM `products` INNER JOIN `sites` ON `sites`.id = `products`.site\_id WHERE ((products.permalink = ‘demarrer-la-virtualisation-avec-xen’) AND (sites.domain\_name = ‘digiprof.eu’)) LIMIT 1

I don’t know how in hell this count(\*) made its way in the SQL!? Is this  
a bug from named\_scope? Everything works fine if there is only 1  
named\_scope in the chain that has a block of code.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [September 25, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/chaining-two-named-scope-that-have-a-block-of-code/149522/2 "2008-09-25T22:08:14Z")

</div>

I solved it by appending the first method to the chain.

I have another problem:

I want to chain 2 named\_scopes that both have a :joins in their  
definition. The problem is that the :joins of the second named\_scope in  
the chain doesn’t get added to the query. IS that normal behavior?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [September 26, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/chaining-two-named-scope-that-have-a-block-of-code/149522/3 "2008-09-26T16:45:55Z")

</div>

Fernando P. wrote:

> I solved it by appending the first method to the chain.
> 
> I have another problem:
> 
> I want to chain 2 named\_scopes that both have a :joins in their  
> definition. The problem is that the :joins of the second named\_scope in  
> the chain doesn’t get added to the query. IS that normal behavior?

I solved that problem too, it was again the select(\*) that was getting  
in the way.

Now another question: can I chain 2 named\_scopes that define the :joins  
option? Is named\_scope smart enough to append the two :joins like it  
does for :conditions? I can’t make it happen. Only one of the :joins  
finds its way in the final query sent to the DB.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [September 26, 2008, 5:41pm UTC](https://www.ruby-forum.com/t/chaining-two-named-scope-that-have-a-block-of-code/149522/4 "2008-09-26T17:41:22Z")

</div>

Actually chaining 2 named\_scopes that have :joins in them is not yet  
possible as of Rails 2.1.1, the issue is fixed and is available in Rails  
edge, or we’ll have to wait until 2.2 is out.
