Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use FactoryBot in db/seeds?

Is it possible to do this?

If so, how can you do it?

Note: FactoryBot was previously named FactoryGirl

like image 979
fespinozacast Avatar asked Mar 21 '11 18:03

fespinozacast


People also ask

What is DB seeds RB?

The seeds.rb file is where the seed data is stored, but you need to run the appropriate rake task to actually use the seed data. Using rake -T in your project directory shows information about following tasks: rake db:seed. Load the seed data from db/seeds.rb.

What is Factorybot Ruby?

Factory Bot, originally known as Factory Girl, is a software library for the Ruby programming language that provides factory methods to create test fixtures for automated software testing.

What does Rails DB seed do?

Rails seed files are a useful way of populating a database with the initial data needed for a Rails project. The Rails db/seeds. rb file contains plain Ruby code and can be run with the Rails-default rails db:seed task.


2 Answers

All you need to do is add require 'factory_bot_rails' to the db/seeds.rb file. This will give you access to your factories.

Note: Gem was previously called FactoryGirlRails

like image 195
Michael Greenly Avatar answered Sep 21 '22 12:09

Michael Greenly


Josh Clayton, the maintainer of FactoryGirl, recommends against using FactoryGirl in your seeds file. He suggests using plain ActiveRecord instead.

like image 28
Dan Croak Avatar answered Sep 21 '22 12:09

Dan Croak