When I create an application and rake task, cannot run Spree:: Image.create!(attachment: File.open (Rails.root + 'app/assets/images/bottle / 1.jpeg'), viewable: product) - ActiveRecord error:: RecordInvalid: Validation failed: Attachment must be present
What kind of information is missing ? Do I need to prescribe additional parameters separately And what kind of validation I can't get through ? ? If check the path manually to the picture, through the rails console, the picture can be seen.
My code
namespace :give_me do
desc 'Load 18 products in database'
task products: :environment do
19.times do |i|
kinds_rand = File.new(Rails.root + 'db/mock_data/kinds.md').readlines.sample
regions_rand = File.new(Rails.root + 'db/mock_data/regions.md').readlines.sample
names_rand = File.new(Rails.root + 'db/mock_data/names.md').readlines.sample
product = Spree::Product.create!(
name: name = names_rand.chomp,
description: "New wine product #{i + 1}",
available_on: Time.zone.now - 1.day,
shipping_category: Spree::ShippingCategory.first,
meta_description: 'Wine, is the best drink in the world',
meta_keywords: %w[wine drink alcohol bottle expensive drink'],
meta_title: name,
price: rand(50..800),
sku: rand(1_000_000..1_999_999),
year: rand(1960..2005),
region: regions_rand.chomp,
alcohol_percentage: rand(5..15),
wine_kind: kinds_rand.chomp
)
Spree::Image.create!(
attachment: File.open(Rails.root + 'app/assets/images/bottle/1.jpeg'), viewable: product
)
end
puts "Here are your products"
end
end
OK, I found decision. Spree::Image.create!(attachment: File.open(Rails.root + 'app/assets/images/bottle/1.jpeg'), viewable: product)
need write on
file_image = File.open(Rails.root.join('app', 'assets', 'images', '1.jpg'))
image = Spree::Image.create!(attachment: { io: file_image, filename: "#{rand(1..20)}.jpg" })
product.images << image
That's all and it's working