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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
588290214f0b0ce2e048d8061bb7d6eb?d=identicon&s=25 Coltz -- (coltz)
on 2009-05-10 05: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
list.
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-05-10 05: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
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)



On Sun, May 10, 2009 at 12:14 AM, Dan Bogdan
569a27ad404519fce3b23d3303894a7e?d=identicon&s=25 Brian (Guest)
on 2009-05-10 06: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
guide: http://guides.rubyonrails.org/association_basics.html
This topic is locked and can not be replied to.