可以访问 Rails 模型的 Ruby 脚本 [英] Ruby scripts with access to Rails Models

查看:45
本文介绍了可以访问 Rails 模型的 Ruby 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里以及如何运行使用我的 rails 环境的简单脚本.具体来说,我有一个包含多条信息的列,我现在为每条信息添加了列,并且需要运行一个 ruby​​ 脚本,该脚本可以运行以调用数据库每一行上的方法来推断数据并将其保存到新列.

Where and how do I run a simple script that uses my rails environment. Specifically I have one column that holds multiple pieces of information, I've added columns now for each piece of information and need to run a ruby script that can run to call a method on each row of the database to extrapolate data and save it to the new column.

推荐答案

这里我必须同意 David 的观点.为此使用迁移.我不确定您想要做什么,但是在您的环境中运行它比手动加载应用程序环境要高效得多,.而且由于您最初的帖子表明您只执行一次,因此迁移是可行的方法:

I have to agree with David here. Use a migration for this. I'm not sure what you want to do, but running it from inside your environment is much, much more efficient then loading up the app environment manually. And since your initial post suggests you're only doing this once, a migration is the way to go:

rails g 迁移 MigrateData

.. 生成:

class MigrateData < ActiveRecord::Migration
  def self.up
    # Your migration code here
  end

  def self.down
    # Rollback scenario
  end
end

当然,您总是希望先在本地执行此操作,使用一些测试数据.

Of course, you will always want to perform this locally first, using some test data.

这篇关于可以访问 Rails 模型的 Ruby 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆