简体   繁体   中英

Updating child association in ActiveModel

I have a Parent object, and it has a Child object as a has_many association.

i get the first child:

s = Parent.first str = s.children.first

and change it:

str.remarks = "something"

now, i would expect s.save to save the child too, but it doesn't. i need to explicitly call str.save, which is bad (because its not in a transaction, and its also ugly).

i've tried marking the relations with :autosave=>true (on both sides) but it does nothing.

what's the standard way of solving this?

i'm working in ROR4, ruby 2.0, if it matters.

Thanks.

In your example, s is not aware of the temporary changes you're making to the record, they are stored in str . If you want this to work instead try

s = Parent.first

s.children.first.remarks = "something"

s.save

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM