I'm implementing Factory Girl as a replacement for fixtures in my Rails app. I have several tables that I'm trying to represent using associations. However, to throw a kink into the loop, beyond just defining the associations, I also need to access attributes of the child factories from the parent.
Below is an example of what I'm trying to do:
Here are the samples:
Factory.define :bar do |e|
e.name "Bar"
end
Factory.define :foo do |e|
e.bar {|b| b.association(:bar)}
end
Factory.define :foo_bar do |b|
f = b.association(:foo)
b.foo_id foo.id
b.bar_id foo.bar_id
end
I've gone through a number of tutorials and other questions and haven't seen any examples of how to do this. Any ideas, or other ways of getting the same result?
Thanks!
EDIT
Based on a couple of the questions, here are some clarifications that I should have included originally...
:foo_bar is not a join table, but a model with other attributes of its own.
This is what I'm actually trying to accomplish:
So, if :foo_bar > :foo > :bar then from :foo_bar, I'm trying to get at :bar's ID.
I'm using an after_build hook to handle situations like this:
Factory.define :foo do |f|
f.name "Foo"
f.association :foo
f.after_build do |obj|
obj.foo_bar = obj.foo.bar
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With