Forum: Ruby on Rails Using :include "recursively" (including the children of the

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 =?ISO-8859-1?Q?Iv=E1n_Vega_Rivera?= (Guest)
on 2005-12-16 21:16
(Received via mailing list)

Let's say I have the following hierarchy of tables:

Group <- parent
Location <- child of Group
School <- child of Location
Phone <- child of School

Then, if I want to do this:

Location.find(...someparms..., :include => [:schools, :group, :phones])

I get an association error (Association was not found). I guess it
happens because AR is looking in the Location model for the Phones
association, which only exists in the School model (I also tried
singular, :phone).

Is there any way I can load all Phone records at once? I wouldn't mind
using custom SQL if that's the only solution.

Thanks in advance!

Ivan V.

PS: Merrrrry christmas!
Cee0292fffa691f1fb320d5400200e99?d=identicon&s=25 Marcel Molina Jr. (Guest)
on 2005-12-16 21:22
(Received via mailing list)
On Fri, Dec 16, 2005 at 02:14:57PM -0600, Iv?n Vega Rivera wrote:
> Is there any way I can load all Phone records at once? I wouldn't mind
> using custom SQL if that's the only solution.

There is no way to do that using the eager loading implementation. It
require custom SQL.

C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 =?ISO-8859-1?Q?Iv=E1n_Vega_Rivera?= (Guest)
on 2005-12-16 21:41
(Received via mailing list)
Marcel Molina Jr. wrote:
Would it be possible to load the phones for the object:

@locs = Location.find(...someparms..., :include => [:schools, :group])

and then something like:

@locs.schools.phones.load_all (of course this method doesn't exist, but
is there something similar?)

Or could I "find" all the phones that belong to certain schools and then
assign those to the specific @locs.schools object?


Ivan V.
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2005-12-20 11:17
(Received via mailing list)
If it's just for the location, then you could include a redundant
key (has_many) to the phones.  Then you could use Location.find(
...someparams :include => [:phones])

Alternativley a method in the Locations model could look something like
the current location

def self.get_phones
       "SELECT phones.* FROM phones p, locations l, schools s
        WHERE = #{id} AND s.location_id = AND p.school_id =")

In which case you could use @some_location.get_phones

going a bit further than that it might be possible to use something like

@phone_numbers = Location.find( :all ).collect {  |loc|

I haven't tested andy of this tho....  Hope it works :)
This topic is locked and can not be replied to.