28848


factory_girlでpogepoge:hoge 1:nの関係を作ろうとしてます。  
下記のように作成したのですが

[code]

    FactoryGirl.define do
      factory :pogepoge do
        association :user, factory: :user
        hoge 1
        birthday Date.today - 20.year
        after(:create) do |pogepoge|
          create(:hoge, pogepoge: pogepoge)
        end
      end
    end
    
[/code]

下記validationでエラーになってしまいます。

[code]

    def hoge_exist
      if hoge.blank?
        errors.add(:hoge, :no_hoge)
      end
    end
    
[/code]

エラーにしない方法を知りたいです。  
エラー内容は以下のような形で出ます。

[code]

     ActiveRecord::RecordInvalid:
       translation missing: ja.activerecord.errors.messages.record_invalid
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/configuration.rb:14:in `block in initialize'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `[]'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:15:in `create'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `tap'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `result'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:5:in `association'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/evaluator.rb:31:in `association'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/attribute/association.rb:19:in `block in to_proc'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/evaluator.rb:71:in `instance_exec'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/evaluator.rb:71:in `block in define_attribute'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:56:in `get'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:12:in `object'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `result'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
     # /Users/hoge/.rbenv/versions/2.2.4/gemsets/ruby224/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
    
[/code]


CC BY-SA 3.0
2016-09-12T12:40:11.080
28840
2016-09-13T02:45:26.377
2016-09-13T01:21:56.867
76
10851
question
ruby-on-rails rspec
factory_girlでhas_manyを作る


解決しました。pogepogeのバリデーションで引っかかったのでbuildに変えました。

[code]

    FactoryGirl.define do
      factory :pogepoge do
        association :user, factory: :user
        hoge 1
        birthday Date.today - 20.year
        after(:build) do |pogepoge|
          pogepoge.hoges.build(attribute_for(:hoge))
        end
      end
    end
    
[/code]


CC BY-SA 3.0
2016-09-13T02:45:26.377
28848
2016-09-13T02:45:26.377
10851
28840
answer