Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access files from a rake task on heroku?

I am trying to seed data in a Rails app, particularly one using carrierwave. I am using a combination of 2 nice solutions found here on stackoverflow!

rake task + yml files for seeding

seeding carrierwave

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 on heroku, I keep getting errors about the path such as

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

I have 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 out using File.join and just concatenation and also trying to use the RAILS_ROOT which seemed to work ok above, but I always seem to get this error.

like image 543
Michael Avatar asked Sep 05 '12 21:09

Michael


1 Answers

Are you creating any files in app/assets/images/objects/ or otherwise putting data there? Does that directory exist in your git repo? You can check if the directory exists on heroku by running heroku run ls app/assets/images/objects/ from the command line.

All apps running on heroku use an ephemeral filesystem:

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.

What this means is that unless you have files in a directory called app/assets/images/objects/ inside your git repository or you're creating those files programatically after your program is deployed, there may not actually be any such files or a directory by that name.

like image 130
culix Avatar answered Nov 15 '22 03:11

culix