ruby on rails 5.2
実現したいこと
FactoryBotで関連付けを正しく設定したい
発生している問題・エラーメッセージ
# /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/attribute_assigner.rb:16:in `block (2 levels) in object' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/attribute_assigner.rb:15:in `each' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/attribute_assigner.rb:15:in `block in object' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/attribute_assigner.rb:14:in `tap' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/attribute_assigner.rb:14:in `object' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/evaluation.rb:13:in `object' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/strategy/create.rb:9:in `result' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/factory.rb:43:in `run' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/factory_runner.rb:29:in `block in run' # /usr/local/bundle/gems/activesupport-5.2.5/lib/active_support/notifications.rb:170:in `instrument' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/factory_runner.rb:28:in `run' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/strategy_syntax_method_registrar.rb:28:in `block in define_singular_strategy_method' # ./spec/requests/apis/v2/hoge.rb:5:in `block (3 levels) in <top (required)>' Finished in 0.62091 seconds (files took 15.52 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/requests/apis/v2/hoge.rb:15 # hogehoge piyopiyo
1対多のモデルをFactoryBotで実装してるところです。
FactoryBotでbelongs_toとhas_manyの関係にあるやつ
上記を参考に無限ループにならないようしたつもりですが、エラーとなります。
※エラーメッセージは大量のせいか?青字だけで肝心の赤字が見えない状態。
該当のソースコード
rspec
※コメントアウトを外すとエラーになる
/spec/factories/smart_lock_providers.rb
ruby
1 2FactoryBot.define do 3 factory :smart_lock_provider do 4 smart_lock_organizations{ 5 [FactoryBot.build(:smart_lock_organization, smart_lock_provider: nil)] 6 } 7 # ↓が問題の部分 8 # smart_lock_doors{ 9 # [FactoryBot.build(:smart_lock_door, smart_lock_provider: nil)] 10 # } 11 end 12end
/spec/factories/smart_lock_organizations.rb
ruby
1FactoryBot.define do 2 factory :smart_lock_organization do 3 smart_lock_provider 4 key { 'key' } 5 name { 'name' } 6 end 7end
/spec/factories/smart_lock_doors.rb
ruby
1FactoryBot.define do 2 factory :smart_lock_door do 3 smart_lock_provider 4 smart_lock_organization 5 key { 'xxxxxx' } 6 name { 'xxxxxx' } 7 end 8end
これを実行するとエラーになる
/spec/requests/apis/v2/hoge.rb
ruby
1require 'rails_helper' 2 3RSpec.describe 'hogehoge', type: :request do 4 describe "piyopiyo" do 5 let!(:smart_lock_door) { create(:smart_lock_door) } 6 it do 7 expect(1 + 2).to eq 3 8 end 9 end 10end 11
model
ruby
1class SmartLockProvider < ActiveRecord::Base 2 has_many :smart_lock_organizations 3 has_many :smart_lock_doors 4end 5
ruby
1class SmartLockOrganization < ActiveRecord::Base 2 belongs_to :smart_lock_provider 3 has_many :smart_lock_doors 4end
ruby
1class SmartLockDoor < ActiveRecord::Base 2 belongs_to :smart_lock_provider, optional: true 3 belongs_to :smart_lock_organization, optional: true 4end
試したこと
/spec/factories/smart_lock_providers.rbのコメントアウト部分を外すと、
letにおいて、親から作っても、子から作ってもエラーになる事を確認。
ruby
1 let!(:smart_lock_provider) { create(:smart_lock_provider) }
ruby
1 let!(:smart_lock_organization) { create(:smart_lock_organization) }
ruby
1 let!(:smart_lock_door) { create(:smart_lock_door) }
逆にコメントアウトすると以下の場合でもエラーは出ない事を確認。
つまりコメントアウト部分の記述が問題だとはわかりましたが、どこが悪いか分からずにいます。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。