I am trying to run a ruby script that parses a text file with album name, artist name, year on each line. It should then save a new record into a Rails model called Album.
(buildDB.rb)
fh = File.open('albums.txt')
while line = fh.gets
if ( line =~ /^(.+)\~\s(.+) \(\'(\d\d)\)/ )
a = Album.new
a.name = $1
a.artist = $2
a.year = $3
a.save
end
end
I am running ruby buildDB.rb in terminal which produces the message
buildDb.rb:12:in '<main>': uninitialized constant Album (NameError)
This made me think that the script could not find the model. So I tried loading the rails environment by using
require "C:/ruby192/www/Project02/config/environment.rb"
at the top of the ruby script. The script will run without errors but nothing is committed to the sqlite database. I can also run find on the already existing Albums, it just seems I can't create new ones.
I am a rails noob so, there probably is a better way to do this (seeds.rb or a rake task maybe). Any help or a direction to look into would be greatly appreciated.