On Wed, Jul 8, 2009 at 1:21 AM, Sijo Kg<[email protected]> wrote:
>
> Hi
>
>  arr = []
>  Feature.find(:all,:conditions => ['name in (?,?,?,?,?,?)','Wheelchair
> Access','Playground','Sandbark','Library','Computer Lab','Testing
> Center']).each {|f| arr = arr && f.schools}
>
>  Now arr have the required value.Is that you want?

First off, I don't think that works.  arr = arr && f.schools will end
up with arr being set only to the set of schools associated with the
last feature instance found.  I think you meant to use Array#& which
performs an array intersection, instead of the && logical operator.

But just changing that will end up with an empty arr since [] & [1] =>
[] i.e the intersection of an empty set with anything is the empty
set.

I believe that something like this is closer to the right meaning:

arr = Schools.find(:all)
Feature.find(:all, :conditions => ['name in (?,?,?,?,?,?)','Wheelchair
Access','Playground','Sandbark','Library','Computer Lab','Testing
Center']).each {|f| arr = arr & f.schools}

That's going to do 2+n queries though, 1 to find the schools, 1 to
find the n features with one of the names, and then a query to find
each of those features schools.

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>


-- 
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

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