Forum: Ruby on Rails renaming associations in polymorphism and versioning

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.
unknown (Guest)
on 2007-08-01 22:11
(Received via mailing list)
Sorry it's such a long one, but hopefully you'll find it an
interesting problem.

I want to keep track of the versions of several of my tables -- at the
moment there are 7, but I may add more as the project develops -- so I
need something a little more DRY than acts_as_versioned. I've finally
come across a solution which I'm happy with the DRYness of (although
it's a little abstract), but the problem is trying to represent some
of my relations effectively. Each record in each of the versioned
tables represents a page on my site, so I've created a pages table to
represent them generically. Here's how the link works:

A Page object belongs to a PageType (such as "employee" or
"workplace"), and a Page has many Revisions. The revisions table
stores stuff like the user who edited the page, the "status" of the
revision and the time. The Revision object is linked polymorphically
to all the different different tables, each representing a page type
and containing the relevant data. So, in this example, there's an
employees table and a workplaces table:

[page_types]--------->[pages]-------->[revisions]<------(polymorphic
association with [employees], [workplaces], etc.)

The problem is, I need an employee to belong_to a workplace, so the
employees table (which holds the revisions of the emplyee data) should
belong_to the pages table. It can't be linked to the workplaces table,
because that holds versions too, and I would have to link to a
specific version, which I don't want to.

Nearly all the page types are linked to another page in some way, so I
thought, rather than make all those belongs_tos, why not just put the
belongs_to in the Revision class. You follow? So now I have the
Revision class belonging to the Page class /twice/. I want the second
association to change name depending on what the page type (or
"revisable_type" field... they hold the same information) says. So as
well as having myEmployeeRevision.page (which would be used for the
first association), I want myEmployee.workplace for the second
association, where workplace is the Page object associated with a
workplace. I'm sure it can be done, but how?
This topic is locked and can not be replied to.