# Eager Loading + Confusion

**URL:** <https://www.ruby-forum.com/t/eager-loading-confusion/133220>\
**Category:** Rails\
**Created:** [March 28, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/eager-loading-confusion/133220 "2008-03-28T23:02:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [March 28, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/eager-loading-confusion/133220/1 "2008-03-28T23:02:08Z")

</div>

So I’m trying to do a find with an order by specific column values  
thing, but some of the columns are attributes through associations.  
So I did some eager loading with the :include option to try and access  
the attributes of the object I’m doing the find on. Stuff has been  
blowing up on me so far.

Here’s the code in the controller right now:

sort\_by = params[:sort\_by] || ‘id’  
direction = params[:direction] || ‘desc’

@state\_agency\_grants = Grant.paginate(:all, :include =\>  
[:funding\_entity, :creator\_entity, applying\_entity], :order=\>  
“#{sort\_by} #{direction}” ,:page =\> params[:page], :per\_page =\> 50)

So yea, passing what to sort by as a parameter, etc  
etc. :funding\_entity is of type FundingEntity while :creator\_entity  
and :appying\_entity are of AccountEntity.

Some of the params[:sort\_by] I’ve passed are:

funding\_entity.name =\> The multi-part identifier “funding\_entity.name”  
could not be bound.

funding\_entity =\> Invalid column name ‘funding\_entity’.

Okay, its checking the database and not the model.

Well, funding\_entities is a table… maybe that will work?

funding\_entities.name =\> Incorrect syntax near ‘.’.: SELECT \* FROM  
(SELECT TOP 5 \* FROM (SELECT TOP 5 [grants].[id] AS t0\_r0, [grants].  
[created\_by] AS t0\_r1, … (40 more of these) … ORDER BY  
funding\_entities.name asc) AS tmp1 ORDER BY funding\_entities.[name]  
DESC) AS tmp2 ORDER BY funding\_entities.[name] asc

Not sure why this happens. Its trying to do funding\_entities.[name]  
or something with escape characters?

I could sort by funding\_entity\_id, but that’s not very useful to the  
end user. I feel like I’m doing it as described here:

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

Any help would be great.

Thanks,  
Brian B.

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [March 31, 2008, 10:45pm UTC](https://www.ruby-forum.com/t/eager-loading-confusion/133220/2 "2008-03-31T22:45:43Z")

</div>

Sorry for bumping this, but I still haven’t been able to figure it  
out.

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [March 31, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/eager-loading-confusion/133220/3 "2008-03-31T22:58:03Z")

</div>

On 31 Mar 2008, at 21:44, Brian B. wrote:

> > funding\_entities.name =\> Incorrect syntax near ‘.’.: SELECT \* FROM  
> > (SELECT TOP 5 \* FROM (SELECT TOP 5 [grants].[id] AS t0\_r0, [grants].  
> > [created\_by] AS t0\_r1, … (40 more of these) … ORDER BY  
> > funding\_entities.name asc) AS tmp1 ORDER BY funding\_entities.[name  
> > ]  
> > DESC) AS tmp2 ORDER BY funding\_entities.[name] asc

The weird bit is here: .[name] it’s like it’s been double escaped.  
What database are you using?  
passing something like funding\_entities.name should work.

Fred

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [March 31, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/eager-loading-confusion/133220/4 "2008-03-31T23:55:57Z")

</div>

Using MSSQL Server. Its weird because it doesn’t try to escape the  
brackets or . (should that even be there?) anywhere else in the  
statement. I’ll inspect the statement again (so long…) and see if  
there’s any reason it does that at the end.

On Mar 31, 3:57 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [April 1, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/eager-loading-confusion/133220/5 "2008-04-01T20:27:40Z")

</div>

Thanks for the good direction. It is indeed in add\_limit\_offset!,  
I’ve done a little bit of modifying but am a bit worried about  
breaking the rest of the application.

There’s something else I don’t completely get either. The model also  
has two associations that use the same table (the Account that created  
it, and the Account the object is for), so the table is  
account\_entities, but the names of the associations are creator\_entity  
and applying\_entity. Right now I’m doing include =\>  
[…, :creator\_entity, :applying\_entity]. Do I want to order by  
account\_entities.name, and if so how do I know it’s applying it to the  
correct column? Is this just the wrong kind of join?

Thanks in advance,  
Brian B.

On Apr 1, 2:44 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [April 2, 2008, 12:43am UTC](https://www.ruby-forum.com/t/eager-loading-confusion/133220/6 "2008-04-02T00:43:11Z")

</div>

MS Sql 2000 does not have limit and offset command

it only appears in MS Sql 2005, but the adapter does not use them

so they need to have a work around

to have the records from 80 to 100 for example

it first does a select limit 100  
then a select in reverserve order limit 80 in the result  
and finally a third select on the result to put in back in initial  
order

this creates some complication with the rest of rails that assumes  
here and there that the code can use limit and offset

i’m not sure about the code but if something is not working as  
expected you have to think of that

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [April 1, 2008, 9:45am UTC](https://www.ruby-forum.com/t/eager-loading-confusion/133220/7 "2008-04-01T09:45:13Z")

</div>

On Mar 31, 10:55 pm, Brian B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Using MSSQL Server. Its weird because it doesn’t try to escape the  
> brackets or . (should that even be there?) anywhere else in the  
> statement. I’ll inspect the statement again (so long…) and see if  
> there’s any reason it does that at the end.

Well I’d guess this funnyness is happening inside the  
add\_limit\_offset! function in the connection adapter. Unfortunately I  
don’t know anything about MSSQL so i’ve no idea if what it’s actually  
trying to accomplish.

Fred
