0
0

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 1 year has passed since last update.

Railsてテスト用のデータを大量に作るメモ

Posted at

FactoryBotで大量のデータを
bulk insertで対応したときのメモ

bulk

def regiregi(foo)
  data = []
  foo.each do |uri|
    data.push(FactoryBot.build(:page, foo: foo, created_at: Time.current, updated_at: Time.current))
  end
  GeneralPage.insert_all data.map(&:attributes);0
end

↑は属性に必要なデータを引数でわたしているのでループでつくってるが
;0はログの出力をしないようにしてる小技みたいなもの

build_listでも大体同じ

def regiregi(foo)
  data =FactoryBot.build_list(:page, foo.count, created_at: Time.current, updated_at: Time.current)
  GeneralPage.insert_all data.map(&:attributes)
end
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?