Getting records from database


#1

hey, i was wondering how to do this

i have these models phones and location

class Phone < ActiveRecord::Base
belongs_to :location
end

class Location < ActiveRecord::Base
has_many :phones
end

when i wanna find all locations, that the model also find the phones for
each
location

Now i get the locations, need only 1 location X, then get all phones for
location X. These are 2 database callings

i wanna reduce this to 1. (perhaps on model level)

can anyone help me?


#2

If you know beforehand that when you load a location you have to access
all
its phones, load it with :include option

Location.find :first, :conditions => “…”, :include => [:phone]

Kent.


#3

Kent S. wrote:

If you know beforehand that when you load a location you have to access all
its phones, load it with :include option

Location.find :first, :conditions => “…”, :include => [:phone]

Just a quick correction… if you want to include all the phone when
retrieving
a location you have to “:include => :phones”. Remember a location has
many phones.

If you wanted to include the location when retrieving a number of phones
you
would “:include => :location”. Because a phone has only 1 location.

-Brian