When there are associated models , it is known that specifying the association in both the models will create a cyclic dependency and causes a “stack level too deep” error . So what’s the correct place to specify the relationship ? Please see these simple associations :
class Patient
has_many :doctors, :through => :join_model
end
class Doctor
has_many :patients, :through => :join_model
end
also
class User
has_many :posts
end
class Post
belongs_to :user
end
In the factories for these models , Which one is the right place to hold the associations ?
there is a section in the factory_girl readme that has an example for a
has_manyassociation: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associationsi think there is no golden rule here. i usually have a default factory per model, that has a simple or no relations set and then i have special factories like
:user_with_poststhat are used for a variety of related tests.i also often just build them up myself in the test themselfes
create(:user, posts: [create(:some_special_post)])