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

  • SEARCH
  • Home
  • 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 3433258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:33:06+00:00 2026-05-18T07:33:06+00:00

I’m doing something slightly unorthodox here, in that I’m just populating the database via

  • 0

I’m doing something slightly unorthodox here, in that I’m just populating the database via a migration, and using the contents of a textfile. I’m using the following method which doesn’t import the entire file, can anyone suggest a solution to this?:

class AddChapters < ActiveRecord::Migration

def self.up

Chapter.create!(:title => "chapter 1",
  :body => File.open("#{Rails.root}/chapters/chapter1.txt").gets)

Chapter.create!(:title => "Chapter 2",
  :body => File.open("#{Rails.root}/chapters/chapter2.txt").gets)

Chapter.create!(:title => "Chapter 3",
  :body => File.open("#{Rails.root}/chapters/chapter3.txt").gets)

end

def self.down
Chapter.all.each do |chapter|
chapter.delete
end
end
end

  • 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-18T07:33:07+00:00Added an answer on May 18, 2026 at 7:33 am

    There could be a number of problems at work here.

    The first one is to check that the body field in your table has sufficient length to hold the contents of the text files.

    Also, gets might not be what you’re after. From the RDoc:

    Reads the next ``line’’ from the I/O stream; lines are separated by sep_string. A separator of nil reads the entire contents, and a zero-length separator reads the input a paragraph at a time (two successive newlines in the input separate paragraphs). The stream must be opened for reading or an IOError will be raised. The line read in will be returned and also assigned to $_. Returns nil if called at end of file.

    What you probably want is IO.read here to guarantee you get all the file, since it can take a path to a file by default, you don’t need to use File at all here:

    Chapter.create!(:title => "chapter 1",
      :body => IO.read("#{Rails.root}/chapters/chapter1.txt"))
    
    Chapter.create!(:title => "Chapter 2",
      :body => IO.read("#{Rails.root}/chapters/chapter2.txt"))
    
    Chapter.create!(:title => "Chapter 3",
      :body => IO.read("#{Rails.root}/chapters/chapter3.txt"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.