Michael Daines
2005-Apr-21 01:42 UTC
Feedback/help on possible ActiveRecord patch/research?
I needed a way to add automatic saving of previous versions of arbitrary tables in my web app''s database, because I wasn''t sure what tables I''d want to do this with and with what fields in them, and I came up with this rough and sort-of tested solution: 1. For the table I want to save previous versions of, I create a table with its singular name, plus "_versions", so that my "articles" table has an "article_versions" counterpart. 2. My "articles" table has a field called "body", and some other fields for its title, what newsletter it belongs to, etc. I want to keep versions of the "body" field only. So, in the "article_versions" table, I create a field called "body", and I also create a field called "article_id", and a field called "created_at". 3. This point is the point at which I need help! What I do here is include a module that has a modified "save" method and methods for getting previous versions of the given article, and create a method in my Article class called "attributes_to_save_in_versions", which returns an array of the fields I want to save in previous versions. Right now, the module is called "KeepsVersionsOf", but I''d like to do something like in ActiveRecord Associations, where I just type "keeps_versions_of :body" or something. The module is here: http://mdaines.com/code/keeps_versions_of.rb (It''s not all that long, but I don''t know the protocol for posting code here, so I thought I better just include a link.) One thing that needs to be done -- if a record is destroyed, the previous versions aren''t. I suppose this would involve doing something to the "destroy" method, but I can''t quite figure out how to use, say, method aliasing to put my destroy code after the existing code without touching it. (Same with the "save" method.) If possible, I would like feedback on this. Also, if possible, advice on how to prepare a patch of it. - Michael Daines
Tim Lucas
2005-Apr-21 02:04 UTC
Re: Feedback/help on possible ActiveRecord patch/research?
On 21/04/2005, at 11:42 AM, Michael Daines wrote:> I needed a way to add automatic saving of previous versions of > arbitrary tables in my web app''s database, because I wasn''t sure what > tables I''d want to do this with and with what fields in them, and I > came up with this rough and sort-of tested solution:Why not use ActiveRecord::Observer to observe the articles on before_update and before_destroy? Combine these with a table view giving you only the articles that have ''DELETED_FLAG = 1'' and vice versa and you''ll be laughing -- unless of course your dbms doesn''t have table views... then life isn''t quite so sweet =) - tim lucas