# Using a Model

**URL:** <https://www.ruby-forum.com/t/using-a-model/174009>\
**Category:** Ruby\
**Created:** [August 19, 2009, 11:27am UTC](https://www.ruby-forum.com/t/using-a-model/174009 "2009-08-19T11:27:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 19, 2009, 11:27am UTC](https://www.ruby-forum.com/t/using-a-model/174009/1 "2009-08-19T11:27:17Z")

</div>

Not sure if this should be on the rails or ruby forum, so my apologies  
if I got it wrong!

So I have created a new rails application, however I want to load data  
from a file into a database table which has a Model associated with it.

My ruby script opens the file, and I want to use the Model to insert the  
data into the database. The script is pretty simple, but it doesn’t  
seem to like loading the model and I created a new folder called  
helper\_scripts in the root of the rails application.

#!/usr/bin/env ruby

require File.dirname( **FILE** ) + ‘/…/app/models/country.rb’  
data\_file = “/tmp/countries”

File.open("/tmp/countries").each { |line|  
data = line.split("\t")  
Country.new  
}

However it fails when trying to load the Model (let alone use it), I  
know this is a total newbie question just need to know how I can use my  
models outside the rails application? or am I doing something  
fundamentally wrong?

Many Thanks

Peter

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 19, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/using-a-model/174009/2 "2009-08-19T14:19:24Z")

</div>

On Wed, Aug 19, 2009 at 5:27 AM, Pete M.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However it fails when trying to load the Model (let alone use it), I

Hi Peter,

Try using script/runner from your rails app and losing the require.  
That will handle loading in your Rails environment. You’ll still have  
to initialize country with the correct values,  
but that will get you going in the right direction.

Jason

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 19, 2009, 2:37pm UTC](https://www.ruby-forum.com/t/using-a-model/174009/3 "2009-08-19T14:37:37Z")

</div>

Thanks Jason,

So all scripts go in there - thats good to know thanks! and is there a  
standard way of running them?

Regards

Peter

Jason S. wrote:

> On Wed, Aug 19, 2009 at 5:27 AM, Pete M.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > However it fails when trying to load the Model (let alone use it), I
> 
> Hi Peter,
> 
> Try using script/runner from your rails app and losing the require.  
> That will handle loading in your Rails environment. You’ll still have  
> to initialize country with the correct values,  
> but that will get you going in the right direction.
> 
> Jason

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 19, 2009, 2:43pm UTC](https://www.ruby-forum.com/t/using-a-model/174009/4 "2009-08-19T14:43:24Z")

</div>

On Wed, Aug 19, 2009 at 8:37 AM, Pete M.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Jason,
> 
> So all scripts go in there - thats good to know thanks! and is there a  
> standard way of running them?
> 
> Regards
> 
> Peter

This is a fairly comprehensive guide to what the scripts are for:

> **[The Rails Command Line — Ruby on Rails Guides](https://guides.rubyonrails.org/command_line.html)**
>
> The Rails Command LineAfter reading this guide, you will know: How to create a Rails application. How to generate models, controllers, database migrations, and unit tests. How to start a development server. How to experiment with objects through an...

Cheers,  
Jason

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 27, 2009, 10:59am UTC](https://www.ruby-forum.com/t/using-a-model/174009/5 "2009-08-27T10:59:33Z")

</div>

Pete M. wrote:

> So I have created a new rails application, however I want to load data  
> from a file into a database table which has a Model associated with it.

Personally, I’d do this from a migration…

You can open the file and manipulate the data from within the migration.  
This gives you the added benefit of being able to unapply-reapply etc  
etc with your other table manipulations.

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 27, 2009, 10:50pm UTC](https://www.ruby-forum.com/t/using-a-model/174009/6 "2009-08-27T22:50:13Z")

</div>

On Wed, Aug 19, 2009 at 4:27 AM, Pete M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However it fails when trying to load the Model (let alone use it), I

I’ve also done this several different times using rake tasks. You could  
do  
something like this

#this file is: lib/tasks/populate\_countries.rake

namespace :populate do

desc “Populate countries from the list in /tmp/countries”  
task :countries =\> :environment do

```
data_file = "/tmp/countries"

File.open("/tmp/countries").each { |line|
   data = line.split("\t")
   Country.new
}

```

end  
end

Then to run it, you just type rake populate:countries

If you are interested in this method, you can watch Railscast episode 66

> **[\#66 Custom Rake Tasks - RailsCasts](http://railscasts.com/episodes/66-custom-rake-tasks)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
