Skip to content

Instantly share code, notes, and snippets.

@beneggett
Last active December 27, 2015 00:49
Show Gist options
  • Save beneggett/7240324 to your computer and use it in GitHub Desktop.
Save beneggett/7240324 to your computer and use it in GitHub Desktop.
File Uploads / Polymorphic Associations Lab

File Uploads / Polymorphic Associations Lab

High level Instructions

  • Add images to your projects & users model using the Paperclip Gem

  • Implement Paperclip by one of three methods: Directly on Model, Through an associated model (Gallery Images), or through polymorphic associations across multiple models.

Polymorphic Associations references:

To set up the association entirely through a generator & set up an index in the migration, do:

rails generate model picture caption description:text pictureable:references

In your migration, make sure you add polymorphic: true

class CreatePictures < ActiveRecord::Migration
  def change
    create_table :pictures do |t|
      t.string :caption
      t.text :description
      t.references :pictureable, polymorphic: true
      t.timestamps
    end
    add_index :pictures, :pictureable_id
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment