Setting an association attribute from another association

23 views
Skip to first unread message

Simone Carletti

unread,
Apr 20, 2013, 5:57:34 AM4/20/13
to factor...@googlegroups.com
Hello list,

I'm trying to find a solution to this problem. I have a Resource model with the following relationships

Resource -> Domain -> User
Resource -> Contact -> User

And this is my declaration

  factory :certificate do
    ...
    association :domain
    association :contact

What I'd like to do, it to be able to specify the user for both associations to be the same. With the current declaration, when the associations are evaluated they result in two different users.

Ideally, I'd like to do something like this

  factory :certificate do
    ...
    association :domain
    association :contact, user_id: domain.user_id

or

  factory :certificate do
    user = ...
    ...
    association :domain, user_id: user.id
    association :contact, user_id: user.id

Any suggestion?

Thanks,
-- Simone

Carlos Chavez

unread,
Apr 27, 2013, 7:18:46 PM4/27/13
to factor...@googlegroups.com
Hi Simone.

Its occurs to me that you can use after_build, for example:

after_build do |
certificate|

    u = FactoryGirl.create(:user)
   
certificate.domain = FactoryGirl.create(:domain, user:u)
    certificate.contact = FactoryGirl.create(:contact, user:u)

    ...
end



--
--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
http://thoughtbot.com/services/training
 
You received this message because you are subscribed to the "factory_girl" mailing list.
To post to this group, send email to factor...@googlegroups.com
To unsubscribe from this group, send email to
factory_girl...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/factory_girl?hl=en
---
You received this message because you are subscribed to the Google Groups "factory_girl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to factory_girl...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Saludos.
Carlos Chávez.
Reply all
Reply to author
Forward
0 new messages