How to pave the way for the image?

Organizationand Imagehave 1-to-1 relationship. Imagehas a column with a name filenamethat saves the file path. I have a file included in the channels app/assets/other/image.jpg. How to include the path to this file when sowing?

I tried in my seed 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 an error:

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

I checked with debuggerand can confirm that it is not @organization, which is zero.
How can I do this work and add the file path to the model Image?


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

When sowing, both attempts cause an error:

CarrierWave::FormNotMultipart: You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.
+4
source share
2

, . , @organization .

file  = File.open(File.join(Rails.root,'app/assets/other/image.jpg'))
image = @organization.build_image(filename: file)
image.save
+6

, ,

1. ,

2. :

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

+3

Source: https://habr.com/ru/post/1606946/


All Articles