ruby - Rails ActiveRecord::Migration - writing a textfile contents to the database -


i'm doing unorthodox here, in i'm populating database via migration, , using contents of textfile. i'm using following method doesn't import entire file, can suggest solution 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 |chapter| chapter.delete end end end

there number of problems @ work here.

the first 1 check body field in table has sufficient length hold contents of text files.

also, gets might not you're after. rdoc:

reads next ``line’’ i/o stream; lines separated sep_string. separator of nil reads entire contents, , zero-length separator reads input paragraph @ time (two successive newlines in input separate paragraphs). stream must opened reading or ioerror raised. line read in returned , assigned $_. returns nil if called @ end of file.

what want io.read here guarantee file, since can take path file default, don't need use file @ 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")) 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -