Sunday, 15 September 2013

ruby on rails - How to create a valid factory with a has_many association -



ruby on rails - How to create a valid factory with a has_many association -

i have been trying work looking @ official docs, this blog post , have looked @ examples on here. whatever seek errors.

has got working illustration @ , see how test structured, using mill girl 4.4.1 , rspec 3

the way have manged create valid object doing this, it's not right way build object want still run validation on animal model first , animal_image

factorygirl.define mill :animal_image animal_id 1 image { file.open("#{rails.root}/spec/fixtures/yp2.jpg") } end end file = factorygirl.create(:animal_image) #<animalimage id: 101, animal_id: 1, image: "yp2.jpg", created_at: "2014-10-14 10:08:55", updated_at: "2014-10-14 10:08:55">

my models

class animal < activerecord::base has_many :animal_images, dependent: :destroy end class animalimage < activerecord::base belongs_to :animal end

any help appreciated

thanks

try this:

factorygirl.define mill :animal ignore images_count 5 end after(:create) |animal, evaluator| create_list(:animal_image, evaluator.images_count, animal: animal) end end end factorygirl.define mill :animal_image image { file.open("#{rails.root}/spec/fixtures/yp2.jpg") } end end = factorygirl.create(:animal) a.animal_images.count => 5 = factorygirl.create(:animal, images_count: 1) a.animal_images.count => 1

more info here

ruby-on-rails factory-girl

No comments:

Post a Comment