LoginSignup
3
3

More than 5 years have passed since last update.

Heroku上で db:seed する時に FactoryGirl を使おうとしたら、Factory not registered になって困った件

Posted at

前提

  • gem 'factory_girl_rails' 使える状態
  • RSpec設定済み
  • Factory置き場は spec/factories
  • 例モデル:User

な状態で以下を進みます。

rake db:seed でダミーデータを生成

factories

spec/factories/user.rb
FactoryGirl.define do
  factory :user do
    sequence :name, "ユーザーn_1"
    sequence(:email){|n| "user#{n}@example.com" }
    password 'hogehoge'
  end
end

seeds

db/seeds.rb
require 'factory_girl'

User.destroy_all

FactoryGirl.create_list(:user, 3)

期待: rake db:seed したら、毎回 users テーブルをまっさらにして、3人ダミーデータをこさえたい。

ダミーデータを生成してみる

$ ./bin/rake db:seed

ローカルでは期待通りだけど、なぜか heroku ではうまく動かない・・

Heroku

$ git push heroku master
$ heroku run rake db:seed

すると、

エラー

... Factory not registered ...

なぜに〜〜;;

解決の糸口

ruby on rails - Rake Aborted Error when pushing to Heroku (factory_girl) - Stack Overflow

Check your
.slugignore
spec/ and test/ directories are often ignored.

意訳: .slugignore されてんじゃない?

されてた!!

spec/

消して、 heroku push, db:seedしたら上手くいった!

おまけ .slugignore ???

Slug Compiler | Heroku Dev Center

When you git push to Heroku, your code is received by the slug compiler which transforms your repository into a slug.

どうもHeroku にコードをプッシュすると、 slug compilerというやつがコードを受け取って、よしなにデプロイしてくれるらしい。 :snail:

参考:Ruby - Herokuのスラグサイズを減らすためにasset_syncを使うやりかた - Qiita

slugのサイズは上限があるらしく、assetsなんかがやたら多いと、超えてしまうそうな。。
そういう時に無視するファイルなんかを指定する為に必要な設定ファイルらしい。

おまけ2: FactoryGirl.definition_file_paths

.slugignore の存在に気づく前に、散々ググってて見かけた FactoryGirl.definition_file_paths にfactoriesディレクト以下の *.rbファイルを読み込めるように指定せよ、という記述を見かけたけど、Railsで db:seed の場合改めて指定し直す必要なくちゃんと読み込めてた。

ただ、 FactoryGirl.definition_file_paths でFactoryGirlが認識するfactoryファイルの場所を確認できたので、「もしかして、Heroku上に読み込もうとしてるFactoryないんじゃない・・?」と気づけたので、.slugignoreじゃなくて、それでもFactoryが見つからない場合、 definition_file_paths がどうなってるか調べると良いかも!

3
3
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
3
3