2012-11-05 2 views
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 : 정의되지 않은 메서드`각 '나는 문제가 당신은 하나의 이미지를 설정하기 위해 노력하고 있다고 생각 #

답변

0

에 대한 has_many 연관에 대해. 만약 당신이 다음과 같이하면 :

factory :food do 

    after(:create) { |f| 
     f.images.create!() 
    } 

end 
+0

나는 위의 코드가 잘 작동해야한다고 생각하지만, 그렇지 않았다. 나에게 이것은 FactoryGirl의 버그 중 하나라고 생각한다. 그리고 ur 대답을위한 thx,하지만 작동하지 않았다. –

관련 문제