class Food < ActiveRecord::Base
has_many :images, as: :imageable, foreign_key: :imageable_uuid, dependent: :destroy
end
class MenuPhoto < ActiveRecord::Base
has_one :image, as: :imageable, foreign_key: :imageable_uuid, dependent: :destroy
end
class Image < ActiveRecord::Base
belongs_to :imageable, foreign_key: :imageable_uuid, :polymorphic => true
end
我上面的这些模型使用多态。所以在我的工厂女孩中,我这样做。
factory :food do
association :images, factory: :image
end
factory :menu_photo do
association :image, factory: :image
end
factory :image do
photo { fixture_file_upload(Rails.root.join("spec", "fixtures", "pass.jpg"), "image/jpg") }
end
当我使用“FactoryGirl.create(:menu_photo)”在 Rails 控制台测试环境中进行测试时,它工作正常。它同时创建:menu_photo 和:image。但是当我用“FactoryGirl.create(:food)”运行它时,它会出错:NoMethodError: undefined method `each' for #