Some days ago, I was requested to enable to load seed data from excel, by my client, in rails 4 project. I told that data to be edited should be managed in database, not excel. But he really requested excel, then I and he decided load data from tsv. Excel is functional, so TSV is appropriate.
Select data area and copy and paste with mouse, Excel data can be pasted as TSV data to text editor, and vise versa.
Environment
Ubuntu 14.04 LTS
Rails 4.1.8
Ruby 2.2.2
Direction
Create feature to load TSV data as a task, and enable to execute with rake.
On executing rake db:seed, execute the task to load TSV.
First, create a task with rails g task sample shot. The following code will be generated.
task
Ruby
1
2
3
4
5
6
namespace:sampledo
desc"shot sample"
taskshot::environmentdo
# something to do
end
end
Now, we can execute the task with bundle exec rake sample:shot. The goal is to execute the task when bundle exec rake db:seed is executed. Maybe, it is the same way to execute task in rails code.
Solution
Add the code into seeds.rb, like below.
seeds.rb
Ruby
1
2
ENV['SOMETHING']='value'
Rake::Task['sample:shot'].invoke
Environment (test, development, production) is passed, which is valid in rake db:seed. On the above, set 'value' to SOMETHING. It is like FEATURES in rake db:fixture:load FIXTURES=xxxx.