Railsアプリ、特にcarrierwaveを使用するアプリにデータをシードしようとしています。私はstackoverflowでここにある2つの素晴らしいソリューションの組み合わせを使用しています!
私は次のものを持っています:
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
ただし、herokuでは、次のようなパスに関するエラーが発生し続けます
rake aborted!
No such file or directory - app/assets/images/objects/house.svg
私はラインのいくつかのバージョンを試しました:
item.filename.store!(File.open(File.join(Rails.root, "app/assets/images/objects/" + item.name.gsub(/ /,'').downcase + ".svg")))
基本的に、File.joinと連結を使用して変更し、上記で正常に機能しているように見えるRAILS_ROOTを使用しようとしましたが、常にこのエラーが発生するようです。