I have a Rails 3.1.x app and need to manipulate the data in it’s database via another straight ruby app. Essentially I am just retrieving data from a model in the database.
Obviously it would start off with something like:
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'mysql2',
:encoding => 'utf8',
:database => 'database_name',
:username => 'username',
:password => 'password',
:host => 'hostname'
)
But where do I go from here with models already set up? Do I import the schema.rb or models?
Would appreciate a more comprehensive use case example of getting a db used in Rails into a standard ruby script.
Rails has rake and script/runner. Both can make it easy to access your models and transparently connect to the database.
I’ve used script/runner when I was retrieving data files from a remote host and needed to insert it into my database underneath the Rails front-end. It worked very nicely.
Read through some of these Stack Overflow questions for more info: