Within my seed.rb I'm populating 4 columns with text data from one title-1.txt file.
title = File.read(File.join(Rails.root, '/features/support/titles/title-1.txt'))
1.upto(4) do |i|
PressRelease.create(
:title => title
)
end
I want to populate each column with different content from different txt files (title-1.txt, title-2.txt, ...)
I know I can make this:
1.upto(4) do |i|
PressRelease.create(
:title => File.read(File.join(Rails.root, '/features/support/titles/title-' + "#{i}" +'.txt'))
)
end
To make it short I've tried changing the title variable into this:
(notice the title-"#{i}")
title = File.read(File.join(Rails.root, '/features/support/titles/title-' + "#{i}" + '.txt'))
But I get this error:
Undefined local variable or method `i' for main:Object
Any tip how to make a reusable and short variable that let's me use it in iterations?
-- UPDATE --
This post doesn't have a complete solution, from the answer of @Paul Fioravanti I got an alternate method to shorten the code. The url must be stored in the variable without the File.open methods, like this:
title_url = '/features/support/titles/title-' + '%s' + '.txt'
1.upto(4) do |i|
PressRelease.create(
:title => File.read(File.join(Rails.root, "#{title_url %i}"))
)
end