Error with Hanami has_many associations (in 1.1.0.beta1)

172 Views Asked by At

I am using Hanami 1.1.0.beta1 (because I need associations).

An event can have many actions:

class EventRepository < Hanami::Repository
  associations do
    has_many :actions
    ...
  end

class ActionRepository < Hanami::Repository
  associations do
    ...
    belongs_to :event
  end

When I call this EventRepository method:

  def add_action(event, data)
    assoc(:actions, event).add(data)
  end

I get this error:

  KeyError:
    key not found: :id
  # /home/eric/.gem/ruby/2.3.4/gems/hanami-model-1.1.0.beta1/lib/hanami/model/associations/has_many.rb:195:in `fetch'
  # /home/eric/.gem/ruby/2.3.4/gems/hanami-model-1.1.0.beta1/lib/hanami/model/associations/has_many.rb:195:in `_build_scope'
  # /home/eric/.gem/ruby/2.3.4/gems/hanami-model-1.1.0.beta1/lib/hanami/model/associations/has_many.rb:47:in `initialize'
  # /home/eric/.gem/ruby/2.3.4/gems/hanami-model-1.1.0.beta1/lib/hanami/model/association.rb:20:in `new'
  # /home/eric/.gem/ruby/2.3.4/gems/hanami-model-1.1.0.beta1/lib/hanami/model/association.rb:20:in `build'
  # /home/eric/.gem/ruby/2.3.4/gems/hanami-model-1.1.0.beta1/lib/hanami/repository.rb:472:in `assoc'
  ...

I looked in has_many.rb. In initialize I see that subject is an Event object and it has an id: HasMany::initialize - subject: #<Event:0x00558f3c198ce8 @id=705, @attributes={:title=>"test_title"}>

but in _build_scope subject has changed to be just the attributes hash, hence the key not found: :id error.

HasMany::_build_scope - subject: {:title=>"test_title"}

which is weird because I don't see any place where subject is changed between initialize and _build_scope...

So is this a bug is 1.1.0.beta1, or am I doing something wrong?

0

There are 0 best solutions below