# Has\_many :through with :order column on the join model

**URL:** <https://www.ruby-forum.com/t/has-many-through-with-order-column-on-the-join-model/100433>\
**Category:** Rails\
**Created:** [July 28, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/has-many-through-with-order-column-on-the-join-model/100433 "2006-07-28T22:01:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [July 28, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/has-many-through-with-order-column-on-the-join-model/100433/1 "2006-07-28T22:01:30Z")

</div>

I have a model like so

class Product \< ActiveRecord::Base  
has\_many :image\_links, :order =\> :position  
has\_many :product\_images,  
:order =\> ‘image\_links.position’,  
:through =\> :image\_links  
end

The problem is that :order clause just gets ignored. How do you server  
records in a specific order when the order column is on the join model?

@product.product\_images produces this SQL:

SELECT product\_images.id, product\_images.created\_at FROM product\_images  
INNER JOIN image\_links ON product\_images.id =  
image\_links.product\_image\_id WHERE (image\_links.product\_id = 1) ORDER BY  
image\_links.position

Whats the problem here?

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [July 28, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/has-many-through-with-order-column-on-the-join-model/100433/2 "2006-07-28T23:42:05Z")

</div>

Alex W. wrote:

> I have a model like so
> 
> class Product \< ActiveRecord::Base  
> has\_many :image\_links, :order =\> :position  
> has\_many :product\_images,  
> :order =\> ‘image\_links.position’,  
> :through =\> :image\_links  
> end
> 
> The problem is that :order clause just gets ignored. How do you server  
> records in a specific order when the order column is on the join model?
> 
> @product.product\_images produces this SQL:
> 
> SELECT product\_images.id, product\_images.created\_at FROM product\_images  
> INNER JOIN image\_links ON product\_images.id =  
> image\_links.product\_image\_id WHERE (image\_links.product\_id = 1) ORDER BY  
> image\_links.position
> 
> Whats the problem here?

Ok, it was apparently working jsut fine and I had a bad test.

Color me embarrased.

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [July 28, 2006, 11:49pm UTC](https://www.ruby-forum.com/t/has-many-through-with-order-column-on-the-join-model/100433/3 "2006-07-28T23:49:41Z")

</div>

I believe that this is covered by ticket #3438

[http://dev.rubyonrails.org/ticket/3438](http://dev.rubyonrails.org/ticket/3438)

Stephen G.
