Storing the order of items in a joined table


#1

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.


#2

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/ (pt-br) | http://blog.codevader.com/
(en)

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


#3

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: http://guides.rubyonrails.org/association_basics.html