我有带有两个模型的 Rails 应用程序
post.rb
class Post < ActiveRecord::Base
# id :integer(11) not null, primary key
# title :text
# body :text
end
和 post_dumps.rb
class PostDump < ActiveRecord::Base
# id :integer(11) not null, primary key
# dupm_file
mount_uploader :dupm_file, DumpUploader # sql file was uploaded
end
如何在 PostDumpsController 中创建这样的方法
class PostDumpsController < ApplicationController
def update_posts_from_dump
@dump = PostDump.find(params[:id])
Post.destroy_all
# run from root user in server
mysql -uroot -ppasswrord production_bd < @dump.dupm_file_url
end
end
最佳答案
Why don't you use rake task for anything like this. Use a location on your server where from where you can dump data into the database. You can even schedule it using cronjob.
注意:我不知道确切的要求,也不知道为什么要这样做,但如果要完成此类操作,那么上面提到的方法是更好的方法。
关于mysql - 如何从服务器中的文件转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44963967/