Forum: Ruby on Rails many to many through and named_scopes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B9638176195f247741cbbb85a2ab44c8?d=identicon&s=25 Jens -- (jc71229)
on 2008-10-26 18:47
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
Fb1a6916e06f221b5b53a6665f9858eb?d=identicon&s=25 Mikel Lindsaar (Guest)
on 2008-10-26 23:40
(Received via mailing list)
On Mon, Oct 27, 2008 at 4:47 AM, Jens --
<rails-mailing-list@andreas-s.net> 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/
Rails, RSpec and Life blog....
B9638176195f247741cbbb85a2ab44c8?d=identicon&s=25 Jens -- (jc71229)
on 2008-10-27 10:34
Mikel Lindsaar wrote:
> On Mon, Oct 27, 2008 at 4:47 AM, Jens --
> <rails-mailing-list@andreas-s.net> 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/
> 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
This topic is locked and can not be replied to.