# Storing the order of items in a joined table

**URL:** <https://www.ruby-forum.com/t/storing-the-order-of-items-in-a-joined-table/167607>\
**Category:** Rails\
**Created:** [May 10, 2009, 5:14am UTC](https://www.ruby-forum.com/t/storing-the-order-of-items-in-a-joined-table/167607 "2009-05-10T05:14:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![coltz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/35a633/32.png) [@coltz](https://www.ruby-forum.com/u/coltz)\
**Post date:** [May 10, 2009, 5:14am UTC](https://www.ruby-forum.com/t/storing-the-order-of-items-in-a-joined-table/167607/1 "2009-05-10T05:14:52Z")

</div>

I am new to rails and I have a question on tables.

I have 2 tables.

1. Points with :id, :name, :x, :y

2. Polygons with :id, :name and an array of points (references to points  
from the Points table). What matters is the order of points in the  
polygon, which follows the order points are linked in the polygon. There  
is a many to many relationship between Points and Polygons through a  
Points\_Polygons table (:point\_id :polygon\_id)

My question is what’s the best way to implement this ordering in Rails?

I thought of adding a :point\_order field to the Points\_Polygons table,  
but this seems very inefficient in case of frequent insertion/deletion  
of points in/from polygons, as each time I insert a point inside a  
polygon I need to update the point\_order field for the points folowing  
the insertion point.

P.S. In the C implementation I have a linked list of Points, and a  
Linked list of Polygons, where each Polygon stores its own list of  
points in a form of a linked list of pointers to points from the Points  
list.

---

<div class="post-metadata">

**Author:** ![coltz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/35a633/32.png) [@coltz](https://www.ruby-forum.com/u/coltz)\
**Post date:** [May 10, 2009, 5:46am UTC](https://www.ruby-forum.com/t/storing-the-order-of-items-in-a-joined-table/167607/2 "2009-05-10T05:46:18Z")

</div>

You can just store it just like the C app code by creating a “points”  
text column and serializing the array of points as a YAML file.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Sun, May 10, 2009 at 12:14 AM, Dan Bogdan

---

<div class="post-metadata">

**Author:** ![coltz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/35a633/32.png) [@coltz](https://www.ruby-forum.com/u/coltz)\
**Post date:** [May 10, 2009, 6:05am UTC](https://www.ruby-forum.com/t/storing-the-order-of-items-in-a-joined-table/167607/3 "2009-05-10T06:05:00Z")

</div>

How about adding a :next\_polygon\_point\_id to your Points\_Polygons  
table and treating it exactly like your linked-list implementation by  
joining the table to itself? See section 2.10 in the associations  
guide: [Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html)
