LoginSignup
2
1

More than 5 years have passed since last update.

rails のautosaveについて

Posted at

has_one のアソシエーション先を同時保存/更新するときはデフォルトだと保存はできますが、更新はできません。(保存のときも小リソースがバリデーションに引っかかると親だけ保存されてるという謎仕様があるっぽい)

autosave: true にしておくと、更新時でも保存できますし、バリデーションもしてくれます。
( validates_associated というものもありますが、autosaveのほうが優秀なのと(追記参照)、2014年くらいから更新されていないので、 autosave の方がいいと思います。)

  • 追記 ソースコードのドキュメントによると、autosaveはトランザクション内で親と小リソースを保存するらしい
Saving of the parent, its associations, and the destruction of marked associations, all happen inside a transaction.

参考URL
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/autosave_association.rb

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1