# Tricky find on associated conditions

**URL:** <https://www.ruby-forum.com/t/tricky-find-on-associated-conditions/171728>\
**Category:** Rails\
**Created:** [July 8, 2009, 8:55am UTC](https://www.ruby-forum.com/t/tricky-find-on-associated-conditions/171728 "2009-07-08T08:55:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [July 8, 2009, 8:55am UTC](https://www.ruby-forum.com/t/tricky-find-on-associated-conditions/171728/1 "2009-07-08T08:55:48Z")

</div>

My head is spinning a bit this morning - can somebody help me with this?

Given these two models with the following associations:

class Article \< ActiveRecord::Base  
belongs\_to :category

- has an attribute called “published\_on”  
end

class Category \< ActiveRecord::Base  
has\_many :articles  
end

I need to list all categories, the order should be determined by the  
“published\_on” date of their latest associated article.

So for example:

cat1 = Category.create  
cat1.articles.create(:published\_on =\> 5.days.ago)  
cat1.articles.create(:published\_on =\> 4.days.ago)

cat2 = Category.create  
cat2.articles.create(:published\_on =\> 2.days.ago)

Then my list should be ordered:

[cat2, cat1]

I believe this would be trivial to make in raw SQL, but I have trouble  
figuring out, how to do it with AR’s find() method.

- Carsten

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [July 8, 2009, 9:43am UTC](https://www.ruby-forum.com/t/tricky-find-on-associated-conditions/171728/2 "2009-07-08T09:43:32Z")

</div>

Carsten G. wrote:

> My head is spinning a bit this morning - can somebody help me with this?
> 
> Given these two models with the following associations:
> 
> class Article \< ActiveRecord::Base  
> belongs\_to :category
> 
> - has an attribute called “published\_on”  
> end
> 
> class Category \< ActiveRecord::Base  
> has\_many :articles  
> end
> 
> I need to list all categories, the order should be determined by the  
> “published\_on” date of their latest associated article.
> 
> So for example:
> 
> cat1 = Category.create  
> cat1.articles.create(:published\_on =\> 5.days.ago)  
> cat1.articles.create(:published\_on =\> 4.days.ago)
> 
> cat2 = Category.create  
> cat2.articles.create(:published\_on =\> 2.days.ago)
> 
> Then my list should be ordered:
> 
> [cat2, cat1]
> 
> I believe this would be trivial to make in raw SQL, but I have trouble  
> figuring out, how to do it with AR’s find() method.
> 
> - Carsten

Hey Carsten

Are you aware that you can include associations in find queries with  
“:include”

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

Off the top of my head, try:

Category.all :include =\> :articles, :order =\> “articles.published\_on  
DESC”

Let me know if that works

Cheers,

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com/)**
>
> Ruby on Rails tips and tutorials

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [July 8, 2009, 10:59am UTC](https://www.ruby-forum.com/t/tricky-find-on-associated-conditions/171728/3 "2009-07-08T10:59:56Z")

</div>

Sweet - wasn’t aware of that one

Thanks

🙂

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com/)**
>
> Ruby on Rails tips and tutorials

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [July 8, 2009, 10:53am UTC](https://www.ruby-forum.com/t/tricky-find-on-associated-conditions/171728/4 "2009-07-08T10:53:24Z")

</div>

Gavin M. wrote:

> Off the top of my head, try:
> 
> Category.all :include =\> :articles, :order =\> “articles.published\_on  
> DESC”

Works like a charm - thanks!

Now that you got my head pointed in the right direction 😉 I noticed,  
that :joins does the same job for me (ie. same result) with some slight  
internal difference:

:include - uses LEFT OUTER JOIN and explicitely selects all attributes  
from both tables (specifying each attribute by name)

:joins - uses INNER JOIN and only selects categories.\*

While it probably won’t matter with my 300-400 articles, in a larger  
dataset I should think, that :joins is the faster of the two.

Thank you for your help. 🙂

- Carsten
