Created
November 24, 2012 11:21
-
-
Save veritas1/4139251 to your computer and use it in GitHub Desktop.
FactoryGirl with polymorphic associations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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