5

Organization and Image have a 1-to-1 relationship. Image has a column called filename, which stores the path to a file. I have such a file included in the asset pipeling: app/assets/other/image.jpg. How can I include the path to this file when seeding?

I have tried in my seeds file:

@organization = ...
@organization.image.create!(filename: File.open('app/assets/other/image.jpg'))
# I also tried:
# @organization.image.create!(filename: 'app/assets/other/image.jpg')

Both generate the error:

NoMethodError: undefined method `create!' for nil:NilClass

I checked with the debugger and can confirm that it's not @organization that is nil.
How can I make this work and add the path to the file to the Image model?


Update: I tried the following:

@image = Image.create!(organization_id: @organization.id,
                       filename: 'app/assets/other/image.jpg')

And I also tried:

image = @organization.build_image(filename: 'app/assets/other/image.jpg')
image.save

Upon seeding both attempts produce the error:

CarrierWave::FormNotMultipart: You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.
Marty
  • 2,132
  • 4
  • 21
  • 47
  • the filename contains a path , so you need to do `@organization.image.create!(filename: 'app/assets/other/image.jpg')` – Kiloreux Sep 11 '15 at 12:09
  • See `# I also tried:` in my post. Isn't that what you mean? If I do that, I get the error upon seeding: `NoMethodError: undefined method 'create!' for nil:NilClass` – Marty Sep 11 '15 at 12:11
  • using File.open is different from using the path directly, and i guess the path is what you really need on the filename attribute – Kiloreux Sep 11 '15 at 12:17
  • Just to be clear: Using `@organization.image.create!(filename: 'app/assets/other/image.jpg')` instead of `@organization.image.create!(filename: File.open('app/assets/other/image.jpg'))` still generates the error `NoMethodError: undefined method `create!' for nil:NilClass`. – Marty Sep 11 '15 at 12:22
  • 1
    @organization.image.create not work with 1-to-1 relation – Gaurav Gupta Sep 11 '15 at 12:40

2 Answers2

7

As your error is clearly telling what the issue is. It turns out that @organization doesn't have any image yet. So try

file  = File.open(File.join(Rails.root,'app/assets/other/image.jpg'))
image = @organization.build_image(filename: file)
image.save
Abdul Baig
  • 3,683
  • 3
  • 21
  • 48
  • Thanks @GB. I tried it but it generated upon seeding the error: `CarrierWave::FormNotMultipart: You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.`. – Marty Sep 11 '15 at 12:54
  • 2
    for more help you can see https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3a-%22Upload%22-from-a-local-file – Abdul Baig Sep 11 '15 at 13:01
3

You are defining one to one relationship between Organization and Image, create action will not work, you have two ways to do this

1. Change association to one to many to make you code working

2. Another one is to do like this:

@image = Image.create(#your code)
@image.organization = @organization

Follow this Link

Community
  • 1
  • 1
Gaurav Gupta
  • 1,181
  • 10
  • 15
  • Thanks! Please see the update I added to the OP, for the error message that option 2 generates for me. – Marty Sep 11 '15 at 12:50