I’m unable to find that on google.
I have 3 models.
model 1 -> has_many model 2 -> has many model 3
model 1 has fields to determine how many model 2 are possible to create. and model 2 has fields to know how many model 3 must be created.
I would like when i save model 1, create automatically model 2 and model 3.
i thinked to use from model 1
after_create create_model2_record
def create_model2_record
for(x=0, x<=model1.field; x++){ #c sample
@model2 = Model2.new
}
end
how can handle this with rails 3?
thansk
Well, this is a basic idea of how it can be done. You can change the code inside create_related_models as you wish, and also choose to use or not to use the condition in after_save. One scenario to use the condition might be a case when need to decide whether to create Model2 and Model3 based on the value of a certain variable in Model1. I hope this works for you. Thanks.