ruby on rails - How can I define multiple associated objects using Factory Girl? -
Offer this syntax to make factory girl docs parents-child associations (I think) ...
Factory Define: Posting | P | P. Authors (| one | A.association (: user)} end
A post belongs to a user (its "author").
What if you Want to define a factory to create a user
s, which contains a group of posts
s?
Or, many-to-many positions (For example, see update below)?
UPDATE
I thought I thought it. I tried it ...
< Code> factory.define (: user) do | f | f.username {factory.neck (User name)} # ... f.roles {| user | [factory (: role), factory (: role, {: name = & gt; 'EDIT_STAFF_DATA'})}}
This worked for the first time, but then I found verification errors because the FG was trying to save the user twice with the same username and email.
Then I asked my basic question But if you have multiple relationships, like like user
and roles
, you A Fakti how something that can define user
will return relevant roles
? Note that roles
should be unique, so I have an F.G. Not every time DB creates a user
to create a new "Admin" role
in DB
I'm not sure that this is the best way to do it, but it works.
Factory.define (: user) do | U | U.login 'my_login' u.password 'test' u.password_confirmation 'test' u.roles {| User | [User.association (: admin_role), user.association (: owner_role ,: authorizable_type = & gt; 'user',: authorizable_id => u.id)]}
Comments
Post a Comment