Since it seems fabricator will always create the associations first, I am wondering how to do a has_one relationship.
class Foo < ActiveRecord::Base
has_one :bar
has_one :baz
end
class Bar < ActiveRecord::Base
belongs_to :foo
validates_presence_of :foo
end
class Baz < ActiveRecord::Base
belongs_to :foo
validates_presence_of :foo
end
Fabricator(:foo)
Fabricator(:foo_with_bar, from: :foo) { bar }
Fabricator(:foo_with_baz, from: :foo) { baz }
Fabricator(:bar)
Fabricator(:baz)
Now it seems when I Fabricate(:foo_with_bar), it will give a validation error saying Bar.foo can't be null. How do I tell the :foo fabricator that it needs to pass itself through to the association in order for validations to pass? I generally don't interact directly with the :bar fabricator at all, so creating the fabrications from the side of the belongs_to at all times seems like I am missing something?
In my case, a Foo doesn't always have a Bar, but a Bar will always have a Foo. As you can tell from the validations. I want to be able to do
foo = Fabricate(:foo_with_bar)
bar = foo.bar # should be a Bar
baz = foo.baz # should be nil
I'm sure I'm just missing something, so if anyone feels the need to shoot me a link and say RTFM, I'm more than happy to.
regards,
Danial