I've been dabbling around in rails and I'm having a hard time understanding how a foreign key automatically get's populated in a table of a model when I push something into it. For example, in a 1:N table relationship of Cart to Ingredients(assuming no carts were made), if I code this:
# create cart and couple of ingredients
cart1 = Cart.create()
jelly = Ingredient.create()
pb = Ingredient.create()
# push jelly, and pb, into cart1
cart1.ingredients << jelly
cart1.ingredients << pb
# pb and jelly cart_id is now populated
pb.cart_id
>>1
jelly.cart_id
>>1
How does rails populate the foreign id, cart_id, column without me having to do anything, except push the ingredient into the cart?