railsでアプリ(rails_appフォルダとする)を作っており、そこに別フォルダ(rails_insertフォルダとする)からデータを流し込んでいます。
rails insertフォルダ内のinsert.rb内に以下の処理を書いてruby insert.rbを実行するとrails_appフォルダにデータが流し込まれるのですが、これはローカル環境なんです。
本番環境にデータを流し込むためには
cur = cur + "/../rails_app/db/development.sqlite3を変えなくてはいけないとおもうのでが、
どのようにしたらいいのでしょうか?
ちなみにherokuで運用しており、DBはpostgresqlです。
↓rails insertフォルダ内のinsert.rb
: : : : def insert(all) cur = File.expand_path(File.dirname(File.dirname(__FILE__))) cur = cur + "/../rails_app/db/development.sqlite3" db = SQLite3::Database.new(cur) all.each do |a| title = a[:title] url = a[:url] image = a[:image] db.execute('insert into articles ("title" ,"url","created_at","updated_at","image") values (?,?,?,?,?)', "#{title}","#{url}",'2018-03-22 21:00:00','2018-03-22 21:00:00',"#{image}") end : : : :
config/database.yml
# SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem 'sqlite3' # default: &default pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> timeout: 5000 development: <<: *default adapter: sqlite3 database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default adapter: sqlite3 database: db/test.sqlite3 production: <<: *default adapter: postgresql database: db/production.sqlite3
ここを参考に以下のように変更して見ましたが、
「could not connect to server: No such file or directory (PG::ConnectionBad)」と出ます。
def insert(all) cur = File.expand_path(File.dirname(File.dirname(__FILE__))) cur = cur + "/../rails_app/db/production.sqlite3" db = PG::connect(cur) all.each do |a| title = a[:title] url = a[:url] image = a[:image] db.exec('insert into articles (title,url,created_at,updated_at,image) values (?,?,?,?,?)', '#{title}','#{url}','2018-03-22 21:00:00','2018-03-22 21:00:00','#{image}') end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/31 02:27
2018/03/31 02:33 編集
2018/03/31 02:36
2018/03/31 07:20 編集