Rails How to use FactoryGirl in Seeds.rb?

与世无争的帅哥 提交于 2020-01-01 03:44:05

问题


I want to have the Seeds.rb file run a method in a file from the Rails.root.join('spec') directory which will fill the database with data generated by FactoryGirl. Lets call this file "helpers.rb" with the method "seed_data"

Edit: require Rails.root.join('spec','helpers.rb') links the file. source

This method I want to use in a before(:all) do seed_data end to seed the test database and rake db:seed for development/production databases with the same effect.


回答1:


Seeds.rb

require Rails.root.join('spec','helpers.rb')
require 'rubygems'           #so it can load gems
require 'factory_girl_rails' #so it can run in development
seed_data

spec/helpers.rb

def seed_data
  Factory.create(:user)
end


来源:https://stackoverflow.com/questions/9881750/rails-how-to-use-factorygirl-in-seeds-rb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!