ActiveRecord Save Dependent Model
        Posted  
        
            by Dmitriy Likhten
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Dmitriy Likhten
        
        
        
        Published on 2010-04-30T02:34:01Z
        Indexed on 
            2010/04/30
            2:37 UTC
        
        
        Read the original article
        Hit count: 366
        
ruby-on-rails
|activerecord
I am trying to save a model with it's dependency models being saved.
Model1
    has_many :model2, :autosave => true
Model2
    belongs_to :model1
    has_many :model3, :autosave => true
Model3
    belongs_to :model2
I want to save Model1, and have Model2 and 3 save as well. I tried this without and with the autosave feature. What winds up happening is Model1 is saved, Model2 is saved, Model3 is untouched. Is there a way to tell ActiveRecord that for this save I want to save the model and all child models all at once?
As a side note, all 3 are just created and are not in the database. I cannot do .create on the models because I cannot save them until all validation passes and all business logic succeeds (has to be a transaction).
© Stack Overflow or respective owner