From: Josh Cheek Date: 2009-08-28T05:49:55+09:00 Subject: Re: Using a Model --000e0cd3580a9b195e047225b592 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Aug 19, 2009 at 4:27 AM, Pete Moran 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. 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 > -- > Posted via http://www.ruby-forum.com/. > > 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 --000e0cd3580a9b195e047225b592--