2017-07-07 66 views
0

我的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 
+0

这看起来像一个疯狂的危险的事情要做。如果你没有别的选择,看看'system'命令。 – tadman

+1

@tadman如果我要为这个话题写一首诗,我会将它命名为“两条反射,作为如何拍摄自己的腿的答案。” – mudasobwa

回答

0

为什么不使用rake任务来做这样的事情。在你的服务器上使用 的位置,你可以将数据转储到数据库。你甚至可以使用cronjob来安排它。

注:我不知道确切的要求,为什么你要这样做,但如果要做这样的事情,那么上面提到的是更好的方法。

相关问题