# Renaming associations in polymorphism and versioning

**URL:** <https://www.ruby-forum.com/t/renaming-associations-in-polymorphism-and-versioning/111001>\
**Category:** Rails\
**Created:** [August 1, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/renaming-associations-in-polymorphism-and-versioning/111001 "2007-08-01T20:11:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 1, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/renaming-associations-in-polymorphism-and-versioning/111001/1 "2007-08-01T20:11:06Z")

</div>

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?
