Iván Vega Rivera
2005-Dec-16 20:14 UTC
Using :include "recursively" (including the children of the child)?
Hi, 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!
Marcel Molina Jr.
2005-Dec-16 20:27 UTC
Re: Using :include "recursively" (including the children of the child)?
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 would require custom SQL. marcel -- Marcel Molina Jr. <marcel-WRrfy3IlpWYdnm+yROfE0A@public.gmane.org>
Iván Vega Rivera
2005-Dec-16 20:39 UTC
Re: Using :include "recursively" (including the children of the child)?
Marcel Molina Jr. wrote:> 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 would > require custom SQL. > > marcel >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? Thanks! Ivan V.
Liquid
2005-Dec-20 10:16 UTC
Re: Using :include "recursively" (including the children of the child)?
If it''s just for the location, then you could include a redundant
foreign
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 for
the current location
def self.get_phones
Phone.find_by_sql(
"SELECT phones.* FROM phones p, locations l, schools s
WHERE l.id = #{id} AND s.location_id = l.id AND p.school_id =
s.id")
end
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|
loc.get_phones}
@phone_numbers.flatten!
I haven''t tested andy of this tho.... Hope it works :)
On 12/16/05, Iván Vega Rivera
<ivanvega-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> Marcel Molina Jr. wrote:
> > 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
> would
> > require custom SQL.
> >
> > marcel
> >
> 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?
>
> Thanks!
>
> Ivan V.
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails