Unless I misunderstood you, I think that would only work if there were only 2 possible service types and I grouped by person_id. I have something a little more general in mind.
On Thursday, August 9, 2012 9:26:24 AM UTC-3, EMoreth wrote: > > Isn't the case where a simple "group by X having count(*) = 2" could > handle the problem ? > > Arel can achieve this query easily. > > On Wed, Aug 8, 2012 at 2:27 PM, Pedro Nascimento > <[email protected]<javascript:> > > wrote: > >> Take a look at ARel and Squeel gem. Squeel gem supports something like >> this already. >> >> On Wed, Aug 8, 2012 at 12:05 PM, Rafael Almeida >> <[email protected]<javascript:> >> > wrote: >> >>> Hello. >>> >>> Let's say I have the following schema >>> >>> People <---> people_services <---> services >>> >>> That is, people has a many-to-many relationship with services. Say I >>> want to find all people who has services of types 1 and 2. How can we do it >>> on activerecord today? >>> >>> My best solution so far is this >>> >>> people_query = Person.joins(:services) >>> people = [] >>> [1,2].each do |t| >>> people << people_query.where(:type => 1) >>> end >>> people = people.reduce(:&) >>> >>> That's not so good because by doing reduce(:&) you're exiting >>> activerecord domain. So you wouldn't be able to do something like >>> people.reduce(:&).limit(10). Also, that way I'm loading a lot more data >>> than I actually need in my application memory. So, certainly not optimal >>> solution. >>> >>> It's possible to make such query in SQL alone, if the dbms supports >>> INTERSECTION then doing it is straightforward. If it doesn't, it's still >>> possible using joins on subqueries. >>> >>> I think activerecord ought support such query and I'm willing to take my >>> time and write the code for it. However, I'd like someone to help me out >>> with it a bit. I don't understand rails or activerecord code base so well. >>> Anyway, it would be best if I did a gem that introduces that feature into >>> activerecord. Can anyone tell me what should I look up? A high level >>> description of what I must do would be great. Something like "You'll need >>> to create a gem, monkey patch this activerecord class, probably use this >>> and this function to help you writing your SQL" would be nice. >>> >>> Cheers >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Ruby on Rails: Core" group. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msg/rubyonrails-core/-/jz3f6lJrXEkJ. >>> To post to this group, send email to >>> [email protected]<javascript:> >>> . >>> To unsubscribe from this group, send email to >>> [email protected] <javascript:>. >>> For more options, visit this group at >>> http://groups.google.com/group/rubyonrails-core?hl=en. >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Core" group. >> To post to this group, send email to >> [email protected]<javascript:> >> . >> To unsubscribe from this group, send email to >> [email protected] <javascript:>. >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-core?hl=en. >> > > > > -- > Att, > Everton > http://www.evertonmoreth.com.br > > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/7eI-cbUHGKoJ. 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-core?hl=en.
