Josh,
This is the default behavior, but it''s kind of tricky depending
on the type of association. Here''s examples:
class One < ActiveRecord::Base
has_many: twos
end
class Two < ActiveRecord::Base
belongs_to: one
end
two = Two.find(1)
two.one.id # Hits to DB loading all of the fields for the
# associated instance of One
two.one_id # (or whatever the foreign key is actually called)
# doesn''t have to hit the DB for this info
one = One.find_first
one.twos[0].id # Hits the DB to get the instance of Two, then looks
# at its id field
# There''s no fields in the record to get the ids of two!
# We have to hit the DB
Thanks,
Chris
> -----Original Message-----
> From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-
> bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of
Josh Knowles
> Sent: Tuesday, May 17, 2005 9:54 PM
> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> Subject: [Rails] Lazy Loading of AR associations
>
> Is there any way currently to lazy-load an AR''s associations such
that
> it only brings back the id''s of the associated objects instead of
the
> entire thing?
>
> For example I have a has-and-belongs-to-many relationship that I want
> to search through for a given id, I do not want the entire child
> record to be retrieved when all I am doing is dealing with the
> relationship table.
>
> Thanks,
>
> Josh
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails