質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.41%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Q&A

解決済

2回答

350閲覧

FactoryBotの関連付けでエラーになる

pecchan

総合スコア591

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

0グッド

0クリップ

投稿2023/09/17 04:01

編集2023/09/17 04:02

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) }

逆にコメントアウトすると以下の場合でもエラーは出ない事を確認。

つまりコメントアウト部分の記述が問題だとはわかりましたが、どこが悪いか分からずにいます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

根本的解決に至ってないですが、こちらの関連を外すことにします。今のところ問題ないため。

ruby

1 smart_lock_organizations{ 2 [FactoryBot.build(:smart_lock_organization, smart_lock_provider: nil)] 3 } 4 # smart_lock_doors{ 5 # [FactoryBot.build(:smart_lock_door, smart_lock_provider: nil)] 6 # }

投稿2023/09/17 23:23

pecchan

総合スコア591

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

自己解決

smart_lock_organizationを追加するマイグレーションの実行漏れでした。
そのためsmart_lock_organization_idを使えませんでした。
大変お騒がせしました。

投稿2023/09/17 23:07

pecchan

総合スコア591

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.41%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問