# Using the model to insert records without a running rails application

**URL:** <https://www.ruby-forum.com/t/using-the-model-to-insert-records-without-a-running-rails-application/171614>\
**Category:** Rails\
**Created:** [July 6, 2009, 5:39pm UTC](https://www.ruby-forum.com/t/using-the-model-to-insert-records-without-a-running-rails-application/171614 "2009-07-06T17:39:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [July 6, 2009, 5:39pm UTC](https://www.ruby-forum.com/t/using-the-model-to-insert-records-without-a-running-rails-application/171614/1 "2009-07-06T17:39:21Z")

</div>

Hey,

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  
environment?

Thanks for your suggestions!  
ms

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [July 6, 2009, 5:44pm UTC](https://www.ruby-forum.com/t/using-the-model-to-insert-records-without-a-running-rails-application/171614/2 "2009-07-06T17:44:36Z")

</div>

On Mon, Jul 6, 2009 at 8:38 AM, ms[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

Use script/runner to execute your script in your Rails environment.  
And of course you can load data from YAML, CSV – whatever kind  
of files you like.

## HTH,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [July 6, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/using-the-model-to-insert-records-without-a-running-rails-application/171614/3 "2009-07-06T17:55:08Z")

</div>

Hi –

On Mon, 6 Jul 2009, ms wrote:

> Hey,
> 
> 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  
> environment?

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  
this:

one:  
name: Emma Peel  
email: mrspeel@blah  
two:  
name: John Steed  
email: steed@blah

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  
end

Then run it using script/runner:

cd my\_rails\_app\_directory  
./script/runner /path/to/read\_records.rb

The runner script will load your application environment (development  
by default), so it will know what User is and it will already have  
YAML loaded.

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.

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Now available: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))  
Training! Intro to Ruby, with Black & Kastner, September 14-17  
(More info: [http://rubyurl.com/vmzN](http://rubyurl.com/vmzN))

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [July 8, 2009, 8:58pm UTC](https://www.ruby-forum.com/t/using-the-model-to-insert-records-without-a-running-rails-application/171614/4 "2009-07-08T20:58:11Z")

</div>

ar\_fixtures can do some of this kind of stuff - may be worth taking a  
look at.

–Matt J.

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [July 6, 2009, 8:18pm UTC](https://www.ruby-forum.com/t/using-the-model-to-insert-records-without-a-running-rails-application/171614/5 "2009-07-06T20:18:56Z")

</div>

Thank you both - this was exactly the hint I needed! 🙂

Cheers,  
ms
