# How to sort on a join... kindof

**URL:** <https://www.ruby-forum.com/t/how-to-sort-on-a-join-kindof/107477>\
**Category:** Rails\
**Created:** [June 22, 2007, 3:47am UTC](https://www.ruby-forum.com/t/how-to-sort-on-a-join-kindof/107477 "2007-06-22T03:47:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 22, 2007, 3:47am UTC](https://www.ruby-forum.com/t/how-to-sort-on-a-join-kindof/107477/1 "2007-06-22T03:47:17Z")

</div>

So i’ve got two models:

foo has\_many bars  
bar belongs\_to foo

And then I can find the newest bar since I’m using nifty created\_at  
columns…

@foo=Foo.find(1)  
@foo.bar.last.whatever

But lets say I have much foo

@foos=Foo.find(:all)  
@foos.each do | f |  
f.bar.last.whatever  
end

But what I really want to do is sort the foos in order of the last bar  
created for each foo. But I can’t figure out how to do it. The  
specifics are topics and posts, and of course I’m trying to do a  
paginating find…

```
@topics_pages, @topics = paginate(:topics, :conditions =>

```

[“group\_id = ?”, @group.id], :order =\> “what in the world do i put  
here…”)

It’s almost like I have to do a join in sql here, but then I end up  
pulling in all the posts in to the array at the time of the find, when  
all I want to do is get the topic so I can later do a topic.post.last

Any great ideas?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 22, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/how-to-sort-on-a-join-kindof/107477/2 "2007-06-22T16:52:47Z")

</div>

OK, so I think I can just get the topics into @topics, and then sort  
the array in rails on topic.post.last, but I don’t know how to do  
it… am I going down the right path here?

—A

On Jun 21, 9:45 pm, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 22, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/how-to-sort-on-a-join-kindof/107477/3 "2007-06-22T17:43:21Z")

</div>

Dave C. wrote:

> * * *
> 
> ## for thing in @things.sort { b.created\_at \<=\> a.created\_at } #loop thru your things and display them end

I probably should have read what I typed. it would be:

* * *

## for thing in @things.sort {|a,b| b.created\_at \<=\> a.created\_at } #loop thru your things and display them end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 22, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/how-to-sort-on-a-join-kindof/107477/4 "2007-06-22T17:54:53Z")

</div>

On Fri, 22 Jun 2007, Dave C. wrote:

> * * *
> 
> ## for thing in @things.sort {|a,b| b.created\_at \<=\> a.created\_at } #loop thru your things and display them end

More idiomatically:  
@things.sort\_by {|x| x.created\_at}.each do |thing|  
#loop through your things and display them  
end

Ought to be quicker, but will use more memory.

## ----------------------------------------------------- Enumerable#sort\_by enum.sort\_by {| obj | block } =\> array

```
 Sorts _enum_ using a set of keys generated by mapping the values in
 _enum_ through the given block.

    %w{ apple pear fig }.sort_by {|word| word.length}
                 #=> ["fig", "pear", "apple"]
    [...]

    Hugh
```

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 22, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/how-to-sort-on-a-join-kindof/107477/5 "2007-06-22T20:10:19Z")

</div>

I’d rather do it in SQL, but I didn’t think :order =\> “created\_at”  
would work… the data being pulled in is a list of topics. I do not  
want to sort on the “created\_at” of the topic, but on the “created\_at”  
of the last post associated with the topic.

Topic 1 was created 2 days ago. It has 2 posts, the newest post is 4  
hours old.  
Topic 2 was created 1 day ago. It has 2 posts, the newest post is 8  
hours old.  
Topic 3 was created 1 hour ago. It has 2 posts, the newest post is 30  
minutes old.

The correct sort order should be 3, 1, 2. If I just do :order =\>  
“created\_at” then the order will be 3, 2, 1.

I am using the .last function to find the last post, but of course  
that only exists in rails. If I specify the post\<-\>topic join in  
the .find then I can do :order =\> “posts.created\_at” but the side  
effect of that is a huge array returned to rails with all the posts  
for a particular topic, when all I need is the created\_at of the .last  
one.

I think I’m looking at a sort in rails, after the list of topics has  
been pulled from the db. Rails’ .sort and sort\_by do not make any  
sense to me yet, so I’ll have to do some reading and figure them  
out. 🙂

—A

On Jun 22, 11:38 am, Dave C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 22, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/how-to-sort-on-a-join-kindof/107477/6 "2007-06-22T17:38:36Z")

</div>

:order =\> “created\_at”  
or  
:order =\> “created\_at DESC”

based on which way u want it sorted.

If you wanted to sort a collection, you could also do:

* * *

## for thing in @things.sort { b.created\_at \<=\> a.created\_at } #loop thru your things and display them end

or you could use sort\_by (see link below).

and for more on sorting:  
[http://redcorundum.blogspot.com/2006/10/using-sortby-instead-of-sort.html](http://redcorundum.blogspot.com/2006/10/using-sortby-instead-of-sort.html)
