Access Child Attributes from Parent Factory Devon Factories

I am implementing Factory Girl as a replacement for fixtures in my Rails application. I have several tables that I'm trying to represent using associations. However, in order to throw a loop into the loop, in addition to simply defining associations, I also need to access the attributes of the subsidiary factories from the parent.

Below is an example of what I'm trying to do:

  • Factory: foo_bar related to Factory: foo related to Factory: bar
  • From: foo_bar, I'm trying to access the attributes of both: foo and: bar

Here are some examples:

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 looked through a number of lessons and other questions and did not see examples of how to do this. Any ideas or other ways to get the same result?

Thank!


EDIT

, , ...

: foo_bar - , .

, :

  • have: foo_bar Factory
  • Factory Factory
  • ( , ): foo_bar Factory

, : foo_bar > : foo > : bar, : foo_bar, : bar ID.

+5
2

after_build :

Factory.define :foo do |f|
  f.name "Foo"
  f.association :foo
  f.after_build do |obj|
    obj.foo_bar = obj.foo.bar
  end
end
+4

, ?

- ?

Factory.define :bar do |f|
    f.name "Bar"
    f.association :foo_bar  
end

Factory.define :foo do |f|
    f.name "Foo"
    f.association :foo_bar  
end

Factory.define :foo_bar do |f|
    f.association :foo
    f.association :bar      
end
0

All Articles