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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mon, Oct 27, 2008 at 4:47 AM, Jens -- <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mikel Lindsaar wrote:> On Mon, Oct 27, 2008 at 4:47 AM, Jens -- > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---