# Many to many through and named\_scopes

**URL:** <https://www.ruby-forum.com/t/many-to-many-through-and-named-scopes/151839>\
**Category:** Rails\
**Created:** [October 26, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/many-to-many-through-and-named-scopes/151839 "2008-10-26T18:47:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [October 26, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/many-to-many-through-and-named-scopes/151839/1 "2008-10-26T18:47:52Z")

</div>

Hi,

I have problem with my named scopes.

A membership has many property\_details through join\_property\_details. I  
do an anonymous named\_scope to get all memberships hat have ‘ns’ and  
‘ae’ as property\_details. However I get always an empty search result.

```
scope = scope.scoped(
 :conditions => ['property_details.short = ?', 'ns'],
 :include => :property_details )
scope = scope.scoped(
 :conditions => ['property_details.short = ?', 'ae'],
 :include => :property_details )

```

SELECT DISTINCT `memberships`.id FROM `memberships`  
LEFT OUTER JOIN `join_property_details` ON (`memberships`.`id` =  
`join_property_details`.`membership_id`)  
LEFT OUTER JOIN `property_details` ON (`property_details`.`id` =  
`join_property_details`.`property_detail_id`)  
WHERE (property\_details.short = ‘ae’) AND (property\_details.short =  
‘ns’)

What do I have to do to get all memberships that have ‘ns’ and ‘ae’ as  
property\_details?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [October 27, 2008, 10:34am UTC](https://www.ruby-forum.com/t/many-to-many-through-and-named-scopes/151839/2 "2008-10-27T10:34:40Z")

</div>

Mikel L. wrote:

> On Mon, Oct 27, 2008 at 4:47 AM, Jens –  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > LEFT OUTER JOIN `property_details` ON (`property_details`.`id` =  
> > `join_property_details`.`property_detail_id`)  
> > WHERE (property\_details.short = ‘ae’) AND (property\_details.short =  
> > ‘ns’)
> > 
> > What do I have to do to get all memberships that have ‘ns’ and ‘ae’ as  
> > property\_details?
> 
> Your problem is that the named scopes ADD to the existing condition…  
> .they limit further and further down the scoping.
> 
> If you look at the generated SQL, you will see this line:
> 
> > WHERE (property\_details.short = ‘ae’) AND (property\_details.short =  
> > ‘ns’)
> 
> Which says all property\_details where the short = ae AND = ns
> 
> As one value can’t be two things at the same time, this will return an  
> empty set.
> 
> You need that line to state:
> 
> WHERE (property\_details.short = ‘ae’) OR (property\_details.short = ‘ns’)
> 
> Swapping the AND for the OR.
> 
> How to do it with your named scope? Well, one way would be to make a  
> scope that accepts a parameter:
> 
> named\_scope :details lambda { |types| :conditions =\>  
> [‘property\_details.short IN (?)’, types],  
> :include =\>  
> :property\_details }
> 
> This takes a param called types which is an array.
> 
> So you could then call:
> 
> Property.details([‘ae’,‘ns’])
> 
> Which should generate what you want.
> 
> You can check out the Railscast on named\_scope to get some more hatting  
> on this.
> 
> YMMV
> 
> Mikel
> 
> –  
> [http://lindsaar.net/](http://lindsaar.net/)  
> Rails, RSpec and Life blog…  
> Hi Mikel,

Thanks for your answer. I think I understand my problem a bit better  
than before, but still not have any solution.

Its right what you said about OR and AND. However OR is not what I want.  
Indeed I know that my property\_details.short column has an entry for  
‘ns’ and ‘ae’, but using an AND will fail because with the joins its  
either ‘ns’ or ‘ae’. The solution would be using an OR and if the query  
returns 2 results for each membership both ‘ns’ and ‘ae’  
property\_details have been found.

I hope this is understandable?! Any idea how to solve this?  
Thanks  
Jens

---

<div class="post-metadata">

**Author:** ![jc71229](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jc71229](https://www.ruby-forum.com/u/jc71229)\
**Post date:** [October 26, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/many-to-many-through-and-named-scopes/151839/3 "2008-10-26T23:40:07Z")

</div>

On Mon, Oct 27, 2008 at 4:47 AM, Jens –  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> LEFT OUTER JOIN `property_details` ON (`property_details`.`id` =  
> `join_property_details`.`property_detail_id`)  
> WHERE (property\_details.short = ‘ae’) AND (property\_details.short =  
> ‘ns’)
> 
> What do I have to do to get all memberships that have ‘ns’ and ‘ae’ as  
> property\_details?

Your problem is that the named scopes ADD to the existing condition…  
.they limit further and further down the scoping.

If you look at the generated SQL, you will see this line:

> WHERE (property\_details.short = ‘ae’) AND (property\_details.short =  
> ‘ns’)

Which says all property\_details where the short = ae AND = ns

As one value can’t be two things at the same time, this will return an  
empty set.

You need that line to state:

WHERE (property\_details.short = ‘ae’) OR (property\_details.short = ‘ns’)

Swapping the AND for the OR.

How to do it with your named scope? Well, one way would be to make a  
scope that accepts a parameter:

named\_scope :details lambda { |types| :conditions =\>  
[‘property\_details.short IN (?)’, types],  
:include =\>  
:property\_details }

This takes a param called types which is an array.

So you could then call:

Property.details([‘ae’,‘ns’])

Which should generate what you want.

You can check out the Railscast on named\_scope to get some more hatting  
on this.

YMMV

Mikel

–

> **[Mikel Lindsaar](https://lindsaar.net/)**
>
> Internet Home for Mikel Lindsaar

Rails, RSpec and Life blog…
