1

When I try to run a Create operation with the trailblazer gem, I get this error:

NoMethodError: undefined method `has_key?' for nil:NilClass
neurodynamic
  • 4,294
  • 3
  • 28
  • 39

1 Answers1

2

The problem was I wasn't including the name of the model as the root key for the hash being passed to the create operation. That is, I was calling it like this:

ModelClass::Create.run(some_attribute: 'attr', another_attribute: 'other_attr')

when I should have been passing:

ModelClass::Create.run(model_class: {some_attribute: 'attr', another_attribute: 'other_attr'})
neurodynamic
  • 4,294
  • 3
  • 28
  • 39
  • See [this issue](https://github.com/apotonick/trailblazer/issues/85) where this issue is discussed. It's due to an assumption made by `validate` that the supplied hash contains a key named after the operation. The idea it's following mirrors how `params` is structured in a typical Rails app. – starfry Feb 09 '16 at 16:27