0
votes

user.rb

 has_many :company_users, :dependent => :destroy
 has_many :companies, through: :company_users

company_user.rb

  belongs_to :company
  belongs_to :user

company.rb

  has_many :company_users, :dependent => :destroy
  has_many :users, through: :company_users

How to write the FactoryGirl configuartion for company.

Whenever company is create company_user is also created with user_id and company_id field. How can I achieve this?

I'm not able to understand to get it.

1
There are associations - zishe

1 Answers

0
votes

This is how I would do it:

factory :company do

  after(:build) do |company|
    company_users = build(:company_users, company: company)
    company.company_users << company_users
  end

end

factory :company_users do
  user
  company
end

Hope that's helpful.