質問

I have a class

class Transaction
    include Mongoid::Document

    belongs_to :objectA
    belongs_to :objectB

    def initialize(objectA, objectB)
        super
        @objectA = objectA
        @objectB = objectB
    end
end

Now I am trying to put together a Fabricator

Fabricator(:transaction) do
     on_init do
        init_with(Fabricate(:klassA), Fabricate(:klassB))
     end
end

I have already created Fabricator(:klassA) and Fabricator(:klassB) which work just fine.

I keep getting this error message:

NoMethodError: undefined method `[]' for #<Fabrication::Schematic::Attribute:0x007f9361b59950>

I am fairly certain that it is related to the initialize method. If I remove the constructor it works.

Mongo version 2.4.1 fabrication (2.6.4) rails (3.2.11)

役に立ちましたか?

解決

That's because your super call will pass your custom arguments to the Mongoid::Document initializer that expects (up to) two hashes, not two objects. The solution is either to call super(), it will pass no arguments, or to rewrite the constructor to pass only additional arguments:

def initialize(objectA, objectB, args = nil, options = nil)
    super(args, options) 
    @objectA = objectA
    @objectB = objectB
end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top