njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Aug-01 18:10 UTC
renaming associations in polymorphism and versioning
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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---