I'm not sure that doing it in SQL is always going to be faster -
there's got to be some performance penalty for doing a join with the
same table that many times.

One favorite trick that can sometimes make things like this more
efficient is to specifically request only the ids of the relevant
objects, do the intersection, and then pull in full objects only for
the resulting set. So something like this (things is an array of
feature names):

record_ids = Feature.find_by_name(things[0]).school_ids
things[1..-1].each do |thing|
  record_ids &= Feature.find_by_name(thing).school_ids
end
@schools = School.find(record_ids)

Note that if you want eliminate some queries, you could pre-collect
the Feature objects with a call to Feature.find(:all, :conditions =>
{ :name => things }), which will find them all in one go.

You'll find other example of this kind of code in the Rails
association preload logic, and several other places from what I
recall.

--Matt Jones



On Jul 8, 7:33 am, Rick DeNatale <[email protected]> wrote:
> Now I think that adding :include => :schools to the Feature.find will
> get this down to 2 queries. But I'm not sure that this will turn out
> to be the most efficient way to do it, since it potentially
> instantiates lots of duplicate School objects and doing the Set
> intersection in Ruby won't be as efficient as letting SQL do it.
>
> Not that I can come up with a way to let SQL do it, without thinking
> harder than I want to this morning. <G>
>

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

Reply via email to