Skip to content

Instantly share code, notes, and snippets.

@veritas1
Created November 24, 2012 11:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veritas1/4139251 to your computer and use it in GitHub Desktop.
Save veritas1/4139251 to your computer and use it in GitHub Desktop.
FactoryGirl with polymorphic associations
FactoryGirl.define do
factory :slideshow_image, class: "Image" do
association :imageable, :factory => :slideshow
# other attributes for slideshow model
end
factory :article_image, class: "Image" do
association :imageable, :factory => :article
# other attributes for article model
end
end
class Image < ActiveRecord::Base
mount_uploader :photo, ImageUploader
belongs_to :imageable, :polymorphic => true
end
class Slideshow < ActiveRecord::Base
has_many :images, :as => :imageable, :dependent => :destroy
end
class Article < ActiveRecord::Base
has_many :images, :as => :imageable, :dependent => :destroy
end
before(:each) do
@article_image = FactoryGirl.create(:article_image)
@article = @article_image.article
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment