Topic: Many models have images... Database advice

In a nutshell: I have 4+ models with images.  I'm using attachment_fu and each image has slightly different properties (just sizes and thumbnails).  What's the best practice for creating tables and models when many models have an image(s)?

Longer:  I moved from keeping images (and music files) in a database to storing them on the file system.  When I started using attachment_fu, I was surprised to find that I was getting double of some models (i.e. 2x all my music collections).  I found out that attachment_fu was placing new rows in my tables for thumbnail images and I decided maybe it was time to move my image data to other tables. 

Now I have 4 models with images (a collection of music, a photo gallery, cartoons, and blog entries) .  These images are offloaded to 4 other models (cover_art, photos, and images, and gallery->photos, respectively) has_one/has_many and belongs_to relationships.  I started looking at polymorphic relationships.  I guess I could have something like:

class Image < ActiveRecord::Base
   belongs_to :imageable, :polymorphic => true
class Collection < ActiveRecord::Base
   has_one :images, :as => :imageable

The problem with this is that I'm not sure how to coax attachment_fu into creating different thumbnails (or lack thereof ) for each parent model.  Also I've read some negative things about Rail's polymorphic models with the words "bad design" floating around.  I really want a decent solution that I won't have to change later and I'm sure there must be a best-practice for this kind of situation.  Also this website is low-scale and basic in nature so I'd really appreciate simplicity. 

Here's a little breakdown showing my current database situation.  I am new at this, so this could be ugly. 

Collection ->     Cover_art (a_fu)
       ->    Songs (a_fu)   

Gallery       ->    Photos (a_fu)

Cartoon   ->    Image(a_fu)

blog_entry ->    Gallery     ->    Photos (a_fu)

All of my tables

    has songs, blog_entries, photos, cartoons

    has songs, cover_art
    Attributes: created_at, title...

    belongs_to collection
    attachment_fu stuff.  No thumbnails yet, maybe in the future. 

    belongs_to user, collection
    Attributes: attachment_fu stuff
    has photos
    Attributes: title, created_at, last_updated_at...

    belongs_to gallery
    Attributes attachment_fu attributes.  Thumbnails in the same table. Caption

    belongs_to user
    has image
    Attributes: created_at, title, caption...

Images (for cartoons)
    belongs to cartoon
    attachment_fu attributes. Thumbnails in the same table

#This is probably bad.  I was going to give each blog entry it's own gallery if it needed it and then hide the gallery on the photo gallery part of the webpage. 
    belongs to user
    has_one gallery

#This is pretty simple, but I would like it to have the option of having images in the future. 
        Attributes: test, title, created_at

Last edited by osteo (2007-03-26 12:17:46)

Re: Many models have images... Database advice

I went ahead and started using image as a polymorphic model.  Some tutorials on this forum really helped out.  I decided to standardize the sizes of my images and their thumbnails and then just use CSS to modify them further. 

Now I'm really struggling with a bug (I think) in attachment_fu that wont' let me use upload_attributes.  But I guess that's for a different area.