Using a Model

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

On Wed, Aug 19, 2009 at 5:27 AM, Pete M.[email protected] 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

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.[email protected] 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

On Wed, Aug 19, 2009 at 8:37 AM, Pete M.[email protected] 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:

Cheers,
Jason

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.

On Wed, Aug 19, 2009 at 4:27 AM, Pete M. [email protected] 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