On Wed, Aug 19, 2009 at 5:27 AM, Pete Moran<pete / zoborg.com> wrote:
> 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
> --
> Posted via http://www.ruby-forum.com/.
>

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