How to create has_and_belongs_to_many associations in Factory girl

Ruby on-RailsAssociationsHas and-Belongs-to-ManyFactory Bot

Ruby on-Rails Problem Overview


Given the following

class User < ActiveRecord::Base
  has_and_belongs_to_many :companies
end

class Company < ActiveRecord::Base
  has_and_belongs_to_many :users
end

how do you define factories for companies and users including the bidirectional association? Here's my attempt

Factory.define :company do |f|
  f.users{ |users| [users.association :company]}
end

Factory.define :user do |f|
  f.companies{ |companies| [companies.association :user]}
end

now I try

Factory :user

Perhaps unsurprisingly this results in an infinite loop as the factories recursively use each other to define themselves.

More surprisingly I haven't found a mention of how to do this anywhere, is there a pattern for defining the necessary factories or I am doing something fundamentally wrong?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Here is the solution that works for me.

FactoryGirl.define do

  factory :company do
    #company attributes
  end

  factory :user do
   companies {[FactoryGirl.create(:company)]}
   #user attributes
  end

end

if you will need specific company you can use factory this way

company = FactoryGirl.create(:company, #{company attributes})
user = FactoryGirl.create(:user, :companies => [company])

Hope this will be helpful for somebody.

Solution 2 - Ruby on-Rails

Factorygirl has since been updated and now includes callbacks to solve this problem. Take a look at http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl for more info.

Solution 3 - Ruby on-Rails

In my opinion, Just create two different factories like:

Factory.define :user, :class => User do |u|

Just normal attributes initialization

end

Factory.define :company, :class => Company do |u|

Just normal attributes initialization

end

When you write the test-cases for user then just write like this

Factory(:user, :companies => [Factory(:company)])

Hope it will work.

Solution 4 - Ruby on-Rails

I couldn´t find an example for the above mentioned case on the provided website. (Only 1:N and polymorphic assocations, but no habtm). I had a similar case and my code looks like this:

Factory.define :user do |user|
 user.name "Foo Bar"
 user.after_create { |u| Factory(:company, :users => [u]) }
end

Factory.define :company do |c|
 c.name "Acme"
end

Solution 5 - Ruby on-Rails

What worked for me was setting the association when using the factory. Using your example:

user = Factory(:user)
company = Factory(:company)

company.users << user 
company.save! 

Solution 6 - Ruby on-Rails

Found this way nice and verbose:

FactoryGirl.define do
  factory :foo do
    name "Foo" 
  end

  factory :bar do
    name "Bar"
    foos { |a| [a.association(:foo)] }
  end
end

Solution 7 - Ruby on-Rails

  factory :company_with_users, parent: :company do
    
    ignore do
      users_count 20
    end
    
    after_create do |company, evaluator|
      FactoryGirl.create_list(:user, evaluator.users_count, users: [user])
    end

  end

Warning: Change users: [user] to :users => [user] for ruby 1.8.x

Solution 8 - Ruby on-Rails

For HABTM I used traits and callbacks.

Say you have the following models:

class Catalog < ApplicationRecord
  has_and_belongs_to_many :coursesend
class Course < ApplicationRecordend

You can define the Factory above:

FactoryBot.define do
  factory :catalog do
    description "Catalog description"
    …

    trait :with_courses do
      after :create do |catalog|
        courses = FactoryBot.create_list :course, 2

        catalog.courses << courses
        catalog.save
      end
    end
  end
end

Solution 9 - Ruby on-Rails

First of all I strongly encourage you to use has_many :through instead of habtm (more about this here), so you'll end up with something like:

Employment belongs_to :users
Employment belongs_to :companies

User has_many :employments
User has_many :companies, :through => :employments 

Company has_many :employments
Company has_many :users, :through => :employments

After this you'll have has_many association on both sides and can assign to them in factory_girl in the way you did it.

Solution 10 - Ruby on-Rails

Update for Rails 5:

Instead of using has_and_belongs_to_many association, you should consider: has_many :through association.

The user factory for this association looks like this:

FactoryBot.define do
  factory :user do
    # user attributes

    factory :user_with_companies do
      transient do
        companies_count 10 # default number
      end

      after(:create) do |user, evaluator|
         create_list(:companies, evaluator.companies_count, user: user)
      end
    end
  end
end

You can create the company factory in a similar way.

Once both factories are set, you can create user_with_companies factory with companies_count option. Here you can specify how many companies the user belongs to: create(:user_with_companies, companies_count: 15)

You can find detailed explanation about factory girl associations here.

Solution 11 - Ruby on-Rails

You can define new factory and use after(:create) callback to create a list of associations. Let's see how to do it in this example:

FactoryBot.define do

  # user factory without associated companies
  factory :user do
    # user attributes

    factory :user_with_companies do
      transient do
        companies_count 10
      end

      after(:create) do |user, evaluator|
        create_list(:companies, evaluator.companies_count, user: user)
      end
    end
  end
end

Attribute companies_count is a transient and available in attributes of the factory and in the callback via the evaluator. Now, you can create a user with companies with the option to specify how many companies you want:

create(:user_with_companies).companies.length # 10
create(:user_with_companies, companies_count: 15).companies.length # 15

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionopsbView Question on Stackoverflow
Solution 1 - Ruby on-RailsSuborxView Answer on Stackoverflow
Solution 2 - Ruby on-RailsopsbView Answer on Stackoverflow
Solution 3 - Ruby on-RailsAshishView Answer on Stackoverflow
Solution 4 - Ruby on-RailsauralbeeView Answer on Stackoverflow
Solution 5 - Ruby on-RailsLarry KooperView Answer on Stackoverflow
Solution 6 - Ruby on-Railspasha.zhukovView Answer on Stackoverflow
Solution 7 - Ruby on-RailsArtur79View Answer on Stackoverflow
Solution 8 - Ruby on-RailslucasarrudaView Answer on Stackoverflow
Solution 9 - Ruby on-RailsMilan NovotaView Answer on Stackoverflow
Solution 10 - Ruby on-RailsNesha ZoricView Answer on Stackoverflow
Solution 11 - Ruby on-RailsAleksandar M.View Answer on Stackoverflow