On Mon, Oct 27, 2008 at 4:47 AM, Jens --
<[EMAIL PROTECTED]> wrote:
> 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
--
http://lindsaar.net/
Rails, RSpec and Life blog....
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---