# :include and conditions

**URL:** <https://www.ruby-forum.com/t/include-and-conditions/91125>\
**Category:** Rails\
**Created:** [April 4, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/include-and-conditions/91125 "2007-04-04T22:36:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [April 4, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/include-and-conditions/91125/1 "2007-04-04T22:36:30Z")

</div>

What is wrong with the following:

@sales\_orders = SalesOrder.find(:all, :include =\> [:histories, :routes],  
:order =\> ‘routes.name ASC’)

That should give me a list of all sales orders sorted by route\_id, which  
comes through the histories table. It doesn’t. They are somewhat  
sorted by route, but not perfectly so. What am I doing wrong? Or is  
there a find\_by\_sql phrase I can use here?

Here’s my models:

class SalesOrder \< ActiveRecord::Base

has\_many :histories  
has\_many :routes,  
:through =\> :histories,  
:order =\> “routes.name asc”

has\_many :histories\_status,  
:order =\> “route\_id”

has\_one :latest\_history,  
:class\_name =\> “History”,  
:order =\> “id desc”

end

class History \< ActiveRecord::Base

belongs\_to :user  
belongs\_to :route  
belongs\_to :sales\_order

end

class Route \< ActiveRecord::Base  
has\_many :histories  
has\_and\_belongs\_to\_many :users  
end

Thanks!
