Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6766503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:50:20+00:00 2026-05-26T14:50:20+00:00

I am trying to run a ruby script that parses a text file with

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:50:21+00:00Added an answer on May 26, 2026 at 2:50 pm

    I would use a rake task:

    task :create_albums => :environment do
      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
      fh.close
    end
    

    I added a ! to the save method so that any errors will throw an exeception. Also, make sure you close your files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two ruby script cron jobs that I'm trying to run under Ubuntu
I'm trying to run a Ruby script using rbenv with cron. I know that
I'm trying to run on my mac os 10.6 a Ruby script that apparently
I have a Ruby script that I built in TextMate and can successfully run
I am trying to run a ruby script which needs root access for some
I'm trying to create a ruby script that checks the real existence of some
I am trying run a program from a qmake .pro file which modifies the
I'm trying to run a batch file, as another user, from my web app.
I'm trying to run a LINQ to SQL query that returns a result in
I'm trying to write a simple ruby script to update my GIT working directories.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.