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?
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. On Friday 09 December 2005 11:25, Brutyn Nick wrote:> 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? > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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