Thanks, Jordan and Matt! I'm running Rails 2.02. Relationships are
as follows:
locations: has many categories through subcategories
categories: has many locations through subcategories
subcategories: belongs to locations and categories
Active record is not filtering out my eagerly loaded subcategories
based on the location, so I'll have to try what you suggested above
which looks like it should work.
Location.find(loc_id, :include => [:subcategories], :conditions =>
["subcategories.location_id = ?", loc_id])
My location and subcategory tables are small, in which case is it
better to do the two queries?, as in
localization = Location.find(loc_id)
subcategories = localization.subcategories
Thanks again!
Cheri
On May 5, 12:06 am, "Jordan Fowler" <[EMAIL PROTECTED]> wrote:
> My response earlier was definitely quick-and-dirty. ActiveRecord (depending
> on the version) should be filtering your joined (eagerly loaded)
> associations for you, based on the association owners id (in your case the
> location id). The query it generates should look something like this:
> SELECT ... FROM `locations` LEFT OUTER JOIN `subcategories` ON
> subcategories.location_id = locations.id WHERE (`locations`.`id` =
> #{loc_id})
>
> Can you help me out by telling me what version of Rails you're running?
>
> --Jordan
>
> On Sun, May 4, 2008 at 3:51 PM, liquid_rails <[EMAIL PROTECTED]>
> wrote:
>
>
>
> > Hi
>
> > Do you know if there is a way to filter eagerly loaded models before
> > they are loaded from the database? Say I want to find a location by
> > its id, and in the same query load only subcategories where
> > subcategory.location_id = location.id. Right now I have this, but all
> > subcategories are getting loaded which is not what I want.
>
> > @location = Location.find(loc_id, :include => [:subcategories])
>
> > Thanks!
>
> --
> Jordan A. Fowler
> 2621 First Ave Apt 5
> San Diego, CA 92103
> E-mail: [EMAIL PROTECTED]
> Website:http://www.jordanfowler.com
> Phone: 406.546.8055
--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---