Forum: Ruby on Rails eager loading : find_with_association

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.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2007-01-21 11:54
in the script I am using, I checked the statement :

f = find_with_associations(options.merge(:sql => sanitized_sql))

where
 sql = "SELECT..."  my select, running fine in sql client mode or
find_by_sql()
and
 options.inspect
=> "{:include=>[:user, :city, {:category=>:domain}]}"

this find_with_associations() just bring me back an empty record

f
=> [#<Proposal:0x3b5ae3c @attributes={"city_id"=>nil, "updated_at"=>nil,
"title"=>nil, "optel"=>nil, "id"=>nil, "show_count"=>nil,
"description"=>nil, "category_id"=>nil, "telephone"=>nil,
"user_id"=>nil,  "valid_until"=>nil, "created_at"=>nil}>]

what's wrong with my nested include ???

tfyh

kad
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-01-21 12:17
(Received via mailing list)
Kad Kerforn wrote:
> in the script I am using, I checked the statement :
>
> f = find_with_associations(options.merge(:sql => sanitized_sql))

That looks like my eager_custom_sql.rb mod.

> => [#<Proposal:0x3b5ae3c @attributes={"city_id"=>nil, "updated_at"=>nil,
> "title"=>nil, "optel"=>nil, "id"=>nil, "show_count"=>nil,
> "description"=>nil, "category_id"=>nil, "telephone"=>nil,
> "user_id"=>nil,  "valid_until"=>nil, "created_at"=>nil}>]
>
> what's wrong with my nested include ???

Are you selecting your fields using the required tn_rn aliasing?


--
We develop, watch us RoR, in numbers too big to ignore.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2007-01-21 12:24
Mark Reginald James wrote:
> Kad Kerforn wrote:
>> in the script I am using, I checked the statement :
>>
>> f = find_with_associations(options.merge(:sql => sanitized_sql))
>
> That looks like my eager_custom_sql.rb mod.
>
>> => [#<Proposal:0x3b5ae3c @attributes={"city_id"=>nil, "updated_at"=>nil,
>> "title"=>nil, "optel"=>nil, "id"=>nil, "show_count"=>nil,
>> "description"=>nil, "category_id"=>nil, "telephone"=>nil,
>> "user_id"=>nil,  "valid_until"=>nil, "created_at"=>nil}>]
>>
>> what's wrong with my nested include ???
>
> Are you selecting your fields using the required tn_rn aliasing?
>
>
> --
> We develop, watch us RoR, in numbers too big to ignore.

the select is like this one :

SELECT *, (6366*acos(cos(0.853291)*cos(cities.latitude) *
cos(cities.longitude - (0.040841))+sin(0.853291)*sin(cities.latitude)))
as km FROM proposals INNER JOIN users ON proposals.user_id = users.id
INNER JOIN cities ON proposals.city_id = villes.id INNER JOIN categories
ON proposals.category_id = categories.id INNER JOIN domains ON
categories.domain_id = domains.id WHERE proposals.city_id IN
(31556,37294,31547,37302,31548,31555,31546,31557,37236,31540,31541) AND
(6366*acos(cos(0.853291)*cos(cities.latitude) *
cos(cities.longitude-(0.040841))+sin(0.853291)*sin(cities.latitude))) <
25.0 AND (valid_until >= '2007-01-21 09:25:15' OR valid_until IS NULL)
ORDER BY km LIMIT 0,4

so, it's a select all, only a calculated value as km..
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-01-21 12:42
(Received via mailing list)
Kad Kerforn wrote:
> (6366*acos(cos(0.853291)*cos(cities.latitude) *
> cos(cities.longitude-(0.040841))+sin(0.853291)*sin(cities.latitude))) <
> 25.0 AND (valid_until >= '2007-01-21 09:25:15' OR valid_until IS NULL)
> ORDER BY km LIMIT 0,4
>
> so, it's a select all, only a calculated value as km..

If you use the :include option you have to specially alias all your
selected fields in the way described in the comment at the top of
the mod, plus use left joins.  And I'm not sure if it's possible to
also select some calculated fields.

Do you really need the returned proposal models to have the included
associated models chained off them, or do you just need to join the
other tables for use in the calculations and conditions?

--
We develop, watch us RoR, in numbers too big to ignore.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2007-01-21 12:53
Mark Reginald James wrote:
> Kad Kerforn wrote:
>> (6366*acos(cos(0.853291)*cos(cities.latitude) *
>> cos(cities.longitude-(0.040841))+sin(0.853291)*sin(cities.latitude))) <
>> 25.0 AND (valid_until >= '2007-01-21 09:25:15' OR valid_until IS NULL)
>> ORDER BY km LIMIT 0,4
>>
>> so, it's a select all, only a calculated value as km..
>
> If you use the :include option you have to specially alias all your
> selected fields in the way described in the comment at the top of
> the mod, plus use left joins.  And I'm not sure if it's possible to
> also select some calculated fields.
>
> Do you really need the returned proposal models to have the included
> associated models chained off them, or do you just need to join the
> other tables for use in the calculations and conditions?
>
> --
> We develop, watch us RoR, in numbers too big to ignore.

In this case I just need to join the other tables for use in the
calculations and conditions,
the resulting list is the proposals list... from there ones can link_to
the proposal.user only
(:cities , :category and :domain models are used only for conditions and
calculations...)

kad
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2007-01-21 13:00
(Received via mailing list)
Kad Kerforn wrote:

> In this case I just need to join the other tables for use in the
> calculations and conditions,
> the resulting list is the proposals list... from there ones can link_to
> the proposal.user only
> (:cities , :category and :domain models are used only for conditions and
> calculations...)

Don't use the :include option then.  Either use find_by_sql, or use a
normal find with specified :select, :joins, :conditions, and :order
options.

--
We develop, watch us RoR, in numbers too big to ignore.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2007-01-21 15:12
Mark Reginald James wrote:
> Kad Kerforn wrote:
>
>> In this case I just need to join the other tables for use in the
>> calculations and conditions,
>> the resulting list is the proposals list... from there ones can link_to
>> the proposal.user only
>> (:cities , :category and :domain models are used only for conditions and
>> calculations...)
>
> Don't use the :include option then.  Either use find_by_sql, or use a
> normal find with specified :select, :joins, :conditions, and :order
> options.
>
> --
> We develop, watch us RoR, in numbers too big to ignore.

thanks that's what I did, running well.....
I've been in touch with Ilya Gregorik who wrote ' Eager Find by SQL
Pagination in Rails' , he never tested w nested include..

is there any info somewhere  about  'when to use eager find.. when not
to use it' .. would be helpful for newbies understanding....
This topic is locked and can not be replied to.