1
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
我有以上這些模型使用多態。所以在我的工廠女孩我這樣做。工廠女孩多態has_many,belongs_to
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)」,它工作正常。它創建了兩個:menu_photo和:image。 但是當我運行「FactoryGirl.create(:食品)」,它與錯誤: NoMethodError:未定義的方法`每個」爲#
我覺得我的代碼應該運行良好,但它沒有。對我而言,我認爲這是FactoryGirl的一個缺陷。和thx爲你的答案,但它沒有奏效。 –