0

Why am i not able to create a fake picture?

Picture is polymorphic to several things including the model proposal:

This is my faker data:

for n in 1..10 do
  picture = Picture.create!(
    picture: Faker::Avatar.image("my-own-slug"),
    imageable_type: 'Proposal',
    imageable_id: n)
end

The imageable type and imageable id get saved correctly I can verify via console. but my picture field remains nil

picture: nil

If just run Faker::Avatar.image("my-own-slug") in my console I get this:

"http://robohash.org/my-own-slug.png?size=300x300&set=set1"

which is what I am looking for. Why is it not working from my seed?

I am seeding alot of other data and it all is working well with Faker except images. Am i missing something?

james
  • 519
  • 3
  • 10
  • 19

1 Answers1

1

I ended up solving this by saving an image to my root folder and just seeding it with its explicit url to my root folder:

for n in 1..10 do
  picture = Picture.create!(
    picture: File.open(File.join(Rails.root, "Joan_Baez_Bob_Dylan_crop.jpg")),
    imageable_type: 'Proposal',
    imageable_id: n)
end
james
  • 519
  • 3
  • 10
  • 19
  • Since passing a file into the picture attribute works when passing a string (from Faker::Avatar.image) didn't, I'm guessing that the Picture model is using Paperclip (or some other file upload gem), and the picture setter expects to receive a file rather than a string. – Benjamin Curtis Jul 23 '16 at 14:28