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