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.
-
Any reason why you wouldn't/shouldn't use a migration to store and execute that code?revdrjrr– revdrjrr2011-05-09 20:51:01 +00:00Commented May 9, 2011 at 20:51
-
no. I dont know how to do it. This is something I like to be able to test before running, so I write these scripts in parts.. show me the objects im going to affect - ill limit to 200 first, then Ill execute it. This is a table with 2million+ rows, so i dont want to just run a migration because if it fails i have to truncate, and re-import. there goes 30 minutes.Rabbott– Rabbott2011-05-09 20:55:39 +00:00Commented May 9, 2011 at 20:55
-
5 years later I feel even more strongly that Rake task is the way to go. check my answer below: stackoverflow.com/a/31738018/726239Shaunak– Shaunak2017-02-10 16:33:44 +00:00Commented Feb 10, 2017 at 16:33
5 Answers
Using a migration sounds like the right way to go if I am understanding your use case.
However, if you really do want to write a standalone script that needs access to your Rails application's models, you can require the environment.rb file from inside your standalone script.
Example:
#!/bin/env ruby
ENV['RAILS_ENV'] = "production" # Set to your desired Rails environment name
require '/path/to/railsapp/config/environment.rb'
# After this point you have access to your models and other classes from your Rails application
model_instance = MyModel.find(7)
model_instance.some_attribute = "new value"
model_instance.save
3 Comments
config/environment.rb file.Agree with everyone, for this specific case it sounds like migration will be way to go, however, to do this regularly, or write some other task/script that interacts rails app environment make rails generate a rake task for you! This gets saved with your rails app, and can be run again and again :)
Easiest way to generate a rake task that interact with rails app/models is to make Rails generate Rake tasks for you!! :)
Here's an example
run
rails g task my_namespace my_taskThis will generate a file called
lib/tasks/my_namespace.rakewhich looks like:
namespace :my_namespace do
desc "TODO: Describe your task here"
task :my_task1 => :environment do
#write any ruby code here and also work with your models
puts User.find(1).name
end
end
Run this task with
rake my_namespace:my_taskWatch your ruby code task that interacts with rails modal run!
Comments
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 migration MigrateData
.. generates:
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.
4 Comments
Seeding data:
Adding data with migrations
Working with Rake Tasks
I prefer to use migrations for adding some data in your case.
Comments
If it's a one-time thing, use a migration.
If this is something that needs to be done multiple times, use a rake task for it.