Eager loading : find_with_association


#1

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


#2

Kad K. 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.


#3

Mark Reginald J. wrote:

Kad K. 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 , (6366acos(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…


#4

Kad K. 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.


#5

Mark Reginald J. wrote:

Kad K. 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


#6

Kad K. 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.


#7

Mark Reginald J. wrote:

Kad K. 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…