[ad_1]
What is the correct way of defining 1:n relations between two models model_a and model_b of the following form?
model_a { id: 1 }
model_b { id: 2 }
model_a 1 : n model_b
case 1: only model_a contains a reference to model_b
model_a { id: 1, model_b_ids: [2] }
model_b { id: 2 }
case 2: only model_b contains a reference to model_a
model_a { id: 1 }
model_b { id: 2, model_a_id: 1 }
case 3: both models referencing each other
model_a { id: 1, model_b_ids: [2] }
model_b { id: 2, model_b_id: 1 }
-
Which relation (
hasOne
,belongsTo
,hasMany
,belongsToMany
, etc) I have to use for each model in each case? -
Which function (
associate
,save
,attach
) if have to use to connect two instances?
[ad_2]