rubyのfixture replacementについての文句
何年ぶりなんだろ。おひさしぶりです。
7年ぶりの投稿は超初心者ネタ。 rubyのfixture replacementってみなさん何使ってますか? まぁ factory_bot ですよね。わたしもそうです。
factory_botって真面目にassociationを表現しようとするとなんか直感的に感じない。。。
class Product < ApplicationRecord has_many :items end class Item < ApplicationRecord belongs_to :product end
というような関連のモデルがあるとき、
FactoryBot.define do factory :poduct, class: 'Product' do items [:low_item, :high_item] # こんな感じに使いたい! end factory :low_item, class: 'Item' do end factory :high_item, class: 'Item' do end end
としたいんですよ。
でも、factory_botのドキュメント では
FactoryBot.define do factory :poduct, class: 'Product' factory :low_item, class: 'Item' do accosiation :product, factory: :product end factory :high_item, class: 'Item' do accosiation :product, factory: :product end end
このようにやりたいことの逆の定義になるっぽい。
ruby tool box で他のライブラリを探してみたんですが、factory_bot一強なんですね。うむー。