Search code examples
ruby-on-railsfactory-bot

Not require FactoryBot preface before create


I am using FactoryBot in my seeds.rb to populate my database. I have to call FactoryBot. before I call build or create. Is there a way I can not have to do that? Also, should I even be using FactoryBot for seeding, or is it just for testing?

Note: FactoryBot was previously called FactoryGirl


Solution

  • Including FactoryBot::Syntax::Methods into your seeds.rb file will allow you to call build or create without the FactoryBot prefix:

    include FactoryBot::Syntax::Methods
    

    As to whether it's a good idea, I can see how it's an appealing option but it seems to me that you'd be maintaining factories for two completely different purposes. Keeping them in sync as your application changes, or separating your test factories from your seed factories, seems problematic to me.

    Note: FactoryBot was previously called FactoryGirl