Reference nested association

92 views
Skip to first unread message

Diego Guebel

unread,
Oct 14, 2013, 11:28:16 PM10/14/13
to fabrica...@googlegroups.com
I'm trying to fabricate different members that belong to the same team - The team's name validates uniqueness.

Using from, I was expecting member2 to use/inherit the same team instances as member. However, the code below fails when trying to fabricate member2 with Validation failed: Name has already been taken.


Fabricator(:team) do
  name 'development' # name must be unique
end

Fabricator(:member) do
  name 'john'
  team
end

Fabricator(:member2, from: :member) do
  name 'george'
end

Is there a way to reference team on member from member2?

Thanks!
D



Paul Elliott

unread,
Oct 14, 2013, 11:39:10 PM10/14/13
to fabrica...@googlegroups.com
The way you have it set up, it will generate a new team every time you create a team member. There are a few ways you could handle this, but the easiest would be to conditionally fabricate the team in the member fabricator, like so:

Fabricator(:member) do
  name 'John'
  team { Team.last || Fabricate(:team) }
end

-- Paul

--
You received this message because you are subscribed to the Google Groups "fabrication" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabricationge...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Diego Guebel

unread,
Oct 15, 2013, 12:19:33 AM10/15/13
to fabrica...@googlegroups.com
Thanks Paul, I think that will do. I'll check it out later from home.

Still curious to know if there is a way to reference the association, as there is no way for me to guarantee that Team.last is the one I want to use.  

Paul Elliott

unread,
Oct 15, 2013, 12:27:36 AM10/15/13
to fabrica...@googlegroups.com
The thing to understand is that you are making schematic for an instance of an object, not a specific instance. Typically I would discourage putting static values in a Fabricator unless they are magic strings. I usually use the `ffaker` gem to populate my data.

If I were making the objects you laid out below, I would do something like this:

```
Fabricator(:team) do
  name { Faker::Company.bs.titleize }
end

Fabricator(:member) do
  team
  name { Faker::Name.name }
end
```

If I needed to create multiple members for a single team, I would then create them like this:

```
team = Fabricate(:team)
Fabricate.times(4, :member, team: team)
```

That would leave me with a randomly named team and 4 randomly named members. I like to provide random test data for things that I don't explicitly care about and only provide specific values when I am asserting on them. If I needed the specific names you provided below, I would override the name field in the schematic in the call to Fabricate.

-- Paul

Diego Guebel

unread,
Oct 15, 2013, 1:26:33 AM10/15/13
to fabrica...@googlegroups.com
RE

team = Fabricate(:team)
Fabricate.times(4, :member, team: team)

This is exactly what I was doing in my Rspec files. I was just wondering if there was a way to do it "more elegantly" in the Fabrication files.

Thanks again,
D

On Tuesday, 15 October 2013 14:28:16 UTC+11, Diego Guebel wrote:
Reply all
Reply to author
Forward
0 new messages