Forum: Ruby on Rails getting records from database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
334ae822566107a2ea2b7a7cb88bd1d6?d=identicon&s=25 brutyn_nick (Guest)
on 2005-12-09 17:38
(Received via mailing list)
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?
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 ksruby (Guest)
on 2005-12-09 18:35
(Received via mailing list)
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.
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 brianvh (Guest)
on 2005-12-09 19:05
(Received via mailing list)
Kent Sibilev 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
This topic is locked and can not be replied to.