# Ordering by associated columns (pleeease help)

**URL:** <https://www.ruby-forum.com/t/ordering-by-associated-columns-pleeease-help/187477>\
**Category:** Rails\
**Created:** [April 23, 2010, 7:28pm UTC](https://www.ruby-forum.com/t/ordering-by-associated-columns-pleeease-help/187477 "2010-04-23T19:28:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sumeet\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Sumeet\_P](https://www.ruby-forum.com/u/Sumeet_P)\
**Post date:** [April 23, 2010, 7:28pm UTC](https://www.ruby-forum.com/t/ordering-by-associated-columns-pleeease-help/187477/1 "2010-04-23T19:28:58Z")

</div>

Hi,

I am stuck pretty bad please help. any comment is helpful.  
I have say three models  
Customer(fields are id, name, cell\_phone, primary\_address\_id)  
belongs\_to :primary\_address, :class\_name =\>  
‘CustomerAddress’, :foreign\_key =\> ‘primary\_address\_id’  
has\_many :customer\_addresses  
end

CustomerAddress(fields city, customer\_id, state)  
belongs\_to :customer  
end

so basically I have many addresses for a customer, and one of which is  
primary address. so I have this relations ship  
customer.primary\_address.

Now I want to write a query by using find or paginate. like:  
Customer.find [arra of id], :order =\> “primary\_address.city ASC”  
The problem is that it is not ordering by associated columns. It says  
no column primary\_address.city found.  
I am trying to use include but it do not works, :join how can I use  
it. Please help is there any way to order by associated columns.

Thanks  
Sumeet P.

---

<div class="post-metadata">

**Author:** ![Sumeet\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Sumeet\_P](https://www.ruby-forum.com/u/Sumeet_P)\
**Post date:** [April 23, 2010, 8:01pm UTC](https://www.ruby-forum.com/t/ordering-by-associated-columns-pleeease-help/187477/2 "2010-04-23T20:01:06Z")

</div>

Sumeet P. wrote:

> Hi,
> 
> I am stuck pretty bad please help. any comment is helpful.  
> I have say three models  
> Customer(fields are id, name, cell\_phone, primary\_address\_id)  
> belongs\_to :primary\_address, :class\_name =\>  
> ‘CustomerAddress’, :foreign\_key =\> ‘primary\_address\_id’  
> has\_many :customer\_addresses  
> end
> 
> CustomerAddress(fields city, customer\_id, state)  
> belongs\_to :customer  
> end
> 
> so basically I have many addresses for a customer, and one of which is  
> primary address. so I have this relations ship  
> customer.primary\_address.
> 
> Now I want to write a query by using find or paginate. like:  
> Customer.find [arra of id], :order =\> “primary\_address.city ASC”  
> The problem is that it is not ordering by associated columns. It says  
> no column primary\_address.city found.  
> I am trying to use include but it do not works, :join how can I use  
> it. Please help is there any way to order by associated columns.
> 
> Thanks  
> Sumeet P.

You can use join as Customer.find(:all, :joins =\> “customers inner join  
customer\_address as b on customers.primary\_address\_id=b.id”) and in this  
you can use ordering.

Anubhaw

---

<div class="post-metadata">

**Author:** ![Sumeet\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Sumeet\_P](https://www.ruby-forum.com/u/Sumeet_P)\
**Post date:** [April 26, 2010, 6:34pm UTC](https://www.ruby-forum.com/t/ordering-by-associated-columns-pleeease-help/187477/3 "2010-04-26T18:34:36Z")

</div>

join will work, even :include will work.  
but this reduce performance for query.
