Making attachment_fu polymorphic

I am working on a small model mixin called attachment_kung to make attachment_fu polymorphic, so you no longer need a different table and Model class for every associated attachment (Productimage, Ad_doc, etc). All you really need is one model and table to handel all your attachments - in some cases, anyway. I have the code working, but have run into one small hitch that I can't seem to ferret out: after the attachment is saved to the db (I am using mySQL 5) and I want to display a list of the attachments, the order they are returned in is strange. I say 'strange' because I, at least, cannot descern what they are ORDERed BY. They seem to come back grouped (an image an its thumbnails will come back adjecent to one another), but the order of the groups (or even the elements within the groups) is unpredictable.

Any help would be appriciated, my code is below.

Thanks in advance, Paul Saieg

Here this is version 0.2 of attachment_kung. It has a few bugs worked out, is a bit faster, but the order of the 'attachments' table is still unpredictable. Any help would be appreciated.

Thanks in advance, Paul Saieg

  #Attachment_kung: Polymorphic Support For Attachment_fu   # Version 0.2   # Originally written by Paul Saieg, 14 September 2007   # email me with comments at classicist at gmail dot com   #   # Licensed under a Creative Commons Attribution-ShareAlike 2.5 License   # CC BY-SA 2.5 Deed | Attribution-ShareAlike 2.5 Generic | Creative Commons

module Attachment_kung   @@attachment_config =   @@validates_as_attachment = nil

  # adds methods to the class that is mixing the module in   def self.append_features(someClass)     @@klass = someClass      def someClass.has_polymorphic_attachment(config)        @@attachment_config = config      end

     def someClass.validates_as_polymorphic_attachment        @@validates_as_attachment = true      end      super   end

  # Gets the full path to the filename in this format:

I am pleased to announce that attachment_kung has been tested and works. The ordering problem had nothing to do with the code, but was the result of using a myISAM table rather than an InnoDB table. An InnoDB table is crucial for the ordering to work. Below is the completed code. Enjoy it, and please let me know if anyone runs into any problems (I haven't tested every possible use case).

- Paul Saieg

module Attachment_kung