I'm using Ruby 2.1.1p76 and Rails 4.0.4 and the Fabrication gem.
Is it possible to refer to the object currently being fabricated?
I have a class Foo and a class Bar. I have fabricators for each. My problem is that each of class Foo and Bar contain a field that refers to the other class:
class Foo < ActiveRecord::Base
has_many :bars
belongs_to :current_bar, class_name: "Bar"
end
class Bar < ActiveRecord::Base
belongs_to: :foo
end
It's bothersome to have to fabricate one, then the other and then set the reference for the first in my specs:
let!( :foo ) { Fabricate( :foo ) }
let!( :bar ) { Fabricate( :bar, foo: foo ) }
before( :each ) do
foo.update( current_bar: bar )
end
I'd much rather just fabricate a Foo and have its current_bar fabricated and already referring to the Foo I'm fabricating. I've read through the fabrication gem documentation and I can't find any way that this is possible. I may just be overlooking it. Does anyone know of a way to accomplish this?
For completeness -- fabricators:
Fabricator( :foo ) do
current_bar nil
end
Fabricator( :bar ) do
foo
end