Forum: Ruby on Rails storing the order of items in a joined table

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Coltz -. (Guest)
on 2009-05-10 07:14
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
Maurício L. (Guest)
on 2009-05-10 07:46
(Received via mailing list)
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 (pt-br) |

On Sun, May 10, 2009 at 12:14 AM, Dan Bogdan
Brian (Guest)
on 2009-05-10 08:05
(Received via mailing list)
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
This topic is locked and can not be replied to.