Create Active Record associations automatically with polymorphic database models
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
attr_accessible :imageable_id, :imageable_type, :name, :url
end
class Document < ActiveRecord::Base
has_many :pictures, :as => :imageable
attr_accessible :title, :content
end
class User < ActiveRecord::Base
has_many :pictures, :as => :imageable
attr_accessible :name, :email, :superpower
end
@document = Document.new({ :title => 'New Document', :content => 'Lorem ipsum' })
# download file, save reponse as @photo
@document.build_picture({ :name => 'Kitty', :url => @photo.path })
@document.save