# Please help me understand.. date not sorting correctly

**URL:** <https://www.ruby-forum.com/t/please-help-me-understand-date-not-sorting-correctly/79173>\
**Category:** Rails\
**Created:** [December 12, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/please-help-me-understand-date-not-sorting-correctly/79173 "2006-12-12T20:56:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickmc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nickmc](https://www.ruby-forum.com/u/nickmc)\
**Post date:** [December 12, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/please-help-me-understand-date-not-sorting-correctly/79173/1 "2006-12-12T20:56:01Z")

</div>

I am new to Ruby and Rails, but have extensive background with php/SQL…  
I am working on my first Ruby project to dive into the framework and  
learn. I am building an internal time tracking app. I have a table of  
jobs, tasks and timelogs.

Timelogs belongs\_to :task, task belongs\_to :jobs and has\_many :timelogs,  
jobs has\_may :tasks and has\_many :timelogs :through =\> :tasks

When pulling all of this data into a detail table i want to sort by the  
task and then by the timelog log\_date.

JOB1  
th: task1  
tr: child timelogs sorted by log\_date

th: task2  
tr: child timelogs sorted by log\_date

My code will sort by the tasks fine, but will not sort by the  
timelog.log\_date no matter what i’ve tried.

Here is part of my job.rb:  
class Job \< ActiveRecord::Base

# create relationships

has\_many :tasks  
has\_many :timelogs, :through =\> :tasks

my action:  
def detailsTable  
@job = Job.find(params[:id], :include =\> [:tasks, :timelogs], :order  
=\> ‘tasks.description, timelogs.log\_date’ )  
end

and my table:  
\<% for task in @job.tasks %\>

 \<% for timelog in task.timelogs %\> \<% end %\>

| \<%=h task.description %\> |
| --- |
| \<%=h timelog.log\_date.strftime("%b %d") %\> | \<%=h timelog.hours %\> hours | \<%=h timelog.description %\> |

\<% end %\>

Any help would be appreciated as I am in need of some clues to this date  
sorting issue. It it probally something in the model that I don’t  
understand.

cheers,  
nick

---

<div class="post-metadata">

**Author:** ![nickmc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nickmc](https://www.ruby-forum.com/u/nickmc)\
**Post date:** [December 12, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/please-help-me-understand-date-not-sorting-correctly/79173/2 "2006-12-12T21:16:00Z")

</div>

development.log will show you the actual SQL queries which may provide  
more insight.

---

<div class="post-metadata">

**Author:** ![nickmc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nickmc](https://www.ruby-forum.com/u/nickmc)\
**Post date:** [December 12, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/please-help-me-understand-date-not-sorting-correctly/79173/3 "2006-12-12T21:56:34Z")

</div>

K… looking at the development log the SQL query appears to be fine, but  
when my view is rendered there is a database query for each task in the  
job.

e[4;36;1mTimelog Load (0.000515)e[0m e[0;1mSELECT \* FROM timelogs  
WHERE (timelogs.task\_id = 12) e[0m  
e[4;35;1mTimelog Load (0.000461)e[0m e[0mSELECT \* FROM timelogs  
WHERE (timelogs.task\_id = 9) e[0m  
e[4;36;1mTimelog Load (0.000290)e[0m e[0;1mSELECT \* FROM timelogs  
WHERE (timelogs.task\_id = 11) e[0m  
e[4;35;1mTimelog Load (0.000311)e[0m e[0mSELECT \* FROM timelogs  
WHERE (timelogs.task\_id = 10) e[0m  
e[4;36;1mTimelog Load (0.000375)e[0m e[0;1mSELECT \* FROM timelogs  
WHERE (timelogs.task\_id = 8) e[0m

That explains why they aren’t sorted. I see that now.

But why are there calls to the db inside of the nested \<% for timelog in  
task.timelogs %\> loop and not the outer \<% for task in @job.tasks %\>  
one. I was trying to minimize the database calls and the my :job symbol  
seems to have everything in it from the originall call.

I can change it do do multiple queries, but i still don’t understand why  
the db is being queried again when all the info is seemingly available.

Like i said, I am a rails virgin… Any info is appreciated.

thanks

---

<div class="post-metadata">

**Author:** ![nickmc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nickmc](https://www.ruby-forum.com/u/nickmc)\
**Post date:** [December 13, 2006, 1:47am UTC](https://www.ruby-forum.com/t/please-help-me-understand-date-not-sorting-correctly/79173/4 "2006-12-13T01:47:24Z")

</div>

Taylor S. wrote:

> add \<%= debug(@job) %\> to your view. This will let us know if  
> ActiveRecord is really pulling all the data into your instance variable  
> in the first query.

did it.

It is showing that everything is being loaded on the first query.

Is there something about this nested for that would make it  
automatically query the db again for the second one? Is there a  
different way to call the :task symbol in the first or second for that  
would make it act different?

@job = Job.find(params[:id], :include =\> [:tasks, :timelogs], :order =\>  
‘tasks.description, timelogs.log\_date’ )

\<% for task in @job.tasks %\>  
…  
\<% for timelog in task.timelogs %\>  
…

Am i just not understanding how rails works? It seem that it should  
have to query a second time in the nested for loop when all the data is  
loaded.

thanks

---

<div class="post-metadata">

**Author:** ![nickmc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nickmc](https://www.ruby-forum.com/u/nickmc)\
**Post date:** [December 12, 2006, 10:37pm UTC](https://www.ruby-forum.com/t/please-help-me-understand-date-not-sorting-correctly/79173/5 "2006-12-12T22:37:35Z")

</div>

add \<%= debug(@job) %\> to your view. This will let us know if  
ActiveRecord is really pulling all the data into your instance variable  
in the first query.

---

<div class="post-metadata">

**Author:** ![nickmc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nickmc](https://www.ruby-forum.com/u/nickmc)\
**Post date:** [December 13, 2006, 2:23am UTC](https://www.ruby-forum.com/t/please-help-me-understand-date-not-sorting-correctly/79173/6 "2006-12-13T02:23:17Z")

</div>

job = Job.find(params[:id], :include =\> [:tasks =\> :timelogs]

Try that. Note the ‘=\>’ denoting the relationship between tasks and  
timelogs.

---

<div class="post-metadata">

**Author:** ![nickmc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nickmc](https://www.ruby-forum.com/u/nickmc)\
**Post date:** [December 13, 2006, 2:38am UTC](https://www.ruby-forum.com/t/please-help-me-understand-date-not-sorting-correctly/79173/7 "2006-12-13T02:38:23Z")

</div>

Ading the ‘=\>’ in the include didn’t help…  
Here is my (hopefully temporary) workaround… i am defining the @timelog  
symbol inside of the view within the first for loop.

\<% for task in @job.tasks %\>  
…  
\<% @timelogs = Timelog.find(:all, :conditions =\> [‘task\_id = ?’,  
task.id], :order =\> ‘log\_date’) %\>  
\<% for timelog in @timelogs %\>  
…  
\<% end %\>  
\<% end %\>

I think there should be a more elegant way, i just am stumbling through  
this right now. It seems that no matter what, according to the  
development.log, the nested for loop would always spawn a db query for  
each iteration. So i made another symbol to loop through so i could set  
the sort order.

I’m am still open for any suggestions, tips, pointers, anything… again  
I’m just starting out.

Thanks for the help

nick

---

<div class="post-metadata">

**Author:** ![nickmc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nickmc](https://www.ruby-forum.com/u/nickmc)\
**Post date:** [December 13, 2006, 2:58am UTC](https://www.ruby-forum.com/t/please-help-me-understand-date-not-sorting-correctly/79173/8 "2006-12-13T02:58:02Z")

</div>

disregard my last post…

adding the ‘=\>’ in the include like you suggested did in fact work.

So now my contoller reads as this  
…  
@job = Job.find(params[:id], :include =\> [:tasks =\> :timelogs], :order  
=\> ‘tasks.description, timelogs.log\_date’ )  
…  
and my nested for loops are working without calling additional db  
queries and everything is being sorted correctly.

Thank you.
