many to many through and named_scopes

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

   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?

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 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