How can I access files from rake task on heroku?

I am trying to sow data in a Rails application, especially using a carrier. I use a combination of two good solutions found here in stackoverflow!

rake task + yml command for seeding

carrier wave seeder

I have the following:

namespace :db do desc "This loads the development data." task :seed => :environment do require 'active_record/fixtures' Dir.glob(RAILS_ROOT + '/db/fixtures/*.yml').each do |file| base_name = File.basename(file, '.*') puts "Loading #{base_name}..." ActiveRecord::Fixtures.create_fixtures('db/fixtures', base_name) end #add in the image file for the default data for item in Item.find(:all) item.filename.store!(File.open(File.join(Rails.root, "app/assets/images/objects/" + item.name.gsub(/ /,'').downcase + ".svg"))) item.save! end end desc "This drops the db, builds the db, and seeds the data." task :reseed => [:environment, 'db:reset', 'db:seed'] end 

However, in heroku, I keep getting errors about the path, for example

 rake aborted! No such file or directory - app/assets/images/objects/house.svg 

I tried several versions of the line:

 item.filename.store!(File.open(File.join(Rails.root, "app/assets/images/objects/" + item.name.gsub(/ /,'').downcase + ".svg"))) 

where I basically changed the use of File.join and just concatenation, and also tried using RAILS_ROOT, which seemed to work fine above, but I always get this error.

+4
source share
1 answer

Do you create files in app/assets/images/objects/ or otherwise put data there? Does this directory exist in your git repository? You can check if the directory exists in heroku by running heroku run ls app/assets/images/objects/ from the command line.

All applications running on heroku use :

Each dyno gets its own ephemeral file system with a fresh copy of the most recently deployed code. During the dynos life cycle, its running processes can use the file system as a temporary notebook, but no files that are written are visible to processes in any other dyno and any recorded files will be discarded when the dyno is stopped or restarted .

This means that if you do not have files in the directory under app/assets/images/objects/ inside your git repository or you create these files programmatically after deploying your program, in fact there cannot be such files or directory by this name.

+3
source

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


All Articles