简体   繁体   中英

factory-girl association has_many through with existing objects

maybe im not phrasing the question right.

I have a data structure that looks like this

A Facility has many users through a Facility role

now in factory gril I know i can create an association like this

factory :facility_role do
  facility {|f| f.association(:facility)}
  user {|u| u.association(:user)}
  role "ABC"
end

but what I want is to associate it to the first facility if there is one otherwise call the create like it dose in the code above

you have access to the currently creating object in your blocks. so you can do what you want in your factory. to make things more clear, you should rename f and u to fr for facility_role.

there are also some callbacks that you can hook into: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#callbacks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM