I want to insert new records manually with a little ruby script and I
want to use the corresponding rails models beyond the running rails
application. How do I manage this? Is there a way to fill up some
tables using a kind of yml-files like you have in the testing
I imagine there's a script or rake task out there that does this, but
if you want to roll your own, it's not hard.
Here's a mini-example. Create a file called (say) users.yml, like
name: Emma Peel
name: John Steed
Then create a file called read_records.rb (or whatever):
users = YAML.load(File.read("/path/to/users.yml"))
users.each do |tag,hash|
User.create(hash) # give or take error checking
Then run it using script/runner:
The runner script will load your application environment (development
by default), so it will know what User is and it will already have
And of course you can include the YAML in the script file instead, or
just create hashes directly, and so on.
As I say, there are probably more turnkey-ish ways to do it already
out there somewhere, though I find it pretty turnkey-ish anyway.
There's also rake db:fixtures:load, if you happen to have the sample
data you want already in the test fixtures.