Rails/ActiveRecord: save changes to a model's associated collections

前端 未结 5 1699
时光取名叫无心
时光取名叫无心 2021-01-05 08:55

Do I have to save modifications to individual items in a collection for a model, or is there a method I can call to save them when I save the model.

#save

5条回答
  •  北海茫月
    2021-01-05 09:40

    just do a rental.dvd.save after you increment the value or in the above case you could use

    rental.dvd.increment!(:copies)
    

    which will also automatically save, note the '!' on increment!

提交回复
热议问题