Associations and Factories

​​Learn about associations, factories, attributes in factories, and factories performance.

The factory_bot has a powerful set of features for adding associations to factories. We’ll learn about them because they are so powerful, and we might see code that uses them. Then we’ll learn about why we should be careful about using them.

Simplest case

The simplest case is also a common one: the class being created has a belongs_to association with the same name as a factory. In that case, we include that name in the factory (these code snippets aren’t in the Rails app we’re working on, for reasons we’ll learn by the end of this section):

Get hands-on with 1400+ tech skills courses.