Using a Model

P

Pete Moran

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
 
J

Jason Stewart

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. =A0The 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 =3D "/tmp/countries"

File.open("/tmp/countries").each { |line|
=A0 =A0data =3D line.split("\t")
=A0 =A0Country.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

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
 
P

Pete Moran

Thanks Jason,

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

Regards

Peter
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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
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
http://railscasts.com/episodes/66-custom-rake-tasks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top