How to add multiple foreign keys to one data structure using build_assoc?

build_assoc/3 accepts a third optional attributes parameter for this purpose:

build_assoc(organization, :employees, location_id: location.id)

P.S. Don’t forget to set foreign key constraints on the database level for data consistency.

4 Likes