18
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

既存DBからseed-fu用seedファイルを生成

18
Last updated at Posted at 2015-09-22

前準備

ActiveRecordクラスだけ作っておく

app/models/city.rb
class City < ActiveRecord::Base
end

既存DB(のSlaveやコピー)に接続する環境を用意しておく
この環境は一時的にあればよい

config/database.yml
dump:
  ...

実行

rails console起動
ダンプ用に作っておいた環境につなぐ

$ RAILS_ENV=dump rails c

rails consoleに以下のコードを打ち込む

SeedFu::Writer.write('db/fixtures/cities.rb', class_name: 'City') do |writer|
  City.order(:id).find_each do |city|
    writer << city.attributes
  end
end

以上w

制御

SeedFu::Writer.writeメソッドにオプション渡すといろいろ制御できる。
例えば、seed_onceで出力したければこんな感じ。

SeedFu::Writer.write('db/fixtures/cities.rb', class_name: 'City', seed_type: :seed_once) do |writer|

他にもオプションあるので、気になる場合はソース見たほうが早い。
https://github.com/mbleigh/seed-fu/blob/master/lib/seed-fu/writer.rb

18
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?