Joining through
Sometimes, you'll need to store additional data related to a HABTM association between your models. In this case, you can use a join-through association type, using a new model to hold the association details.
Join-through associations are similar to HABTM, but instead of having one table that stores the IDs of the associated models, we also store additional data related to the specific association. For example, if we follow our package's HABTM warehouses example from the previous recipe, we might want to store the number of packages we have stored in each warehouse, thus adding a new field to our association table to specify the required stock number for each association,.
Getting ready
Let's define a stock per package in each warehouse and redefine our HABTM association from our previous recipe to use a join-through association instead.
Note that we are using the tables and models created in our previous recipe.
How to do it...
Perform the following steps:
First, add a new
amount...