<p>I have a Rails app with some basic models. The website displays data retrieved from other sources. So I need to write a Ruby script that creates new instances in my database. I know I can do that with the test hooks, but I'm not sure that makes sense here.</p>

<p>I'm not sure what this task should look like, how I can invoke it, or where it should go in my source tree (lib\tasks?).</p>

<p>For example, here's my first try:</p>

<pre><code>require 'active_record'
require '../app/models/mymodel.rb'

test = MyModel.new
test.name = 'test'
test.save
</code></pre>

<p>This fails because it can't get a connection to the database. This makes sense in a vague way to my newbie brain, since presumably Rails is doing all the magic work behind the scenes to set all that stuff up. So how do I set up my little script?</p>

You can open a connection in your scripts as such:
```
```
ActiveRecord::Base.establish_connection(
    :adapter  => "mysql",
    :username => "root",
    :host     => "localhost",
    :password => "******",
    :database => "******" 
)
```
```
I'm sure there is a more elegant way to do it, so that it grabs the info from your database.yml.