Jordan meant to say:

Location.find(loc_id, :include => [:subcategories], :conditions =>
["subcategories.location_id = ?", loc_id])

Depending on the size of your tables it might be better to do:

localization = Location.find(loc_id)
subcategories = localization.subcategories

Quite often 2 small queries are faster than 1 slow one. (and joining tables
make your query slightly slower)

Also, note that my code above is easier to read and therefore easier to
maintain.

-Matt

-Matt

On 5/4/08, Jordan Fowler <[EMAIL PROTECTED]> wrote:
>
> Oops, in that conditions, make sure it's "locations.id".
>
> On Sun, May 4, 2008 at 4:04 PM, Jordan Fowler <[EMAIL PROTECTED]> wrote:
>
> > Hi there,
> > Does your Location model currently have the below association?
> >
> > has_many :subcategories
> >
> > If not, ActiveRecord won't know you're trying to limit the subcategories
> > to the current Location. If this isn't an option, you can do the following:
> >
> > Location.find(loc_id, :include => [:subcategories], :conditions =>
> > ["subcategories.location_id = location.id"])
> >
> > Hope this helps.
> >
> > --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
> >
>
>
>
> --
> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to