说我有一个Foo
模型。一个Foo
has_many Bar
。Bar
商店value
。AFoo
将多个Bar
对象存储为一个combo
对象。
例如:
f = Foo.find(2)
f.combo
# combo is essentially Bar.find_by(foo: f).pluck(:value).join(" ")
# I want to be able to easily retrieve (like above)
# create/edit/update
f.combo = "moo cow"
# all related existing Bar objects should be updated,
# and new additions should be created,
# and no longer relevant ones should be deleted
f.save
# delete
f.combo = nil
# all related Bar objects should be deleted
f.save
有没有一种方法可以轻松地完成上述逻辑?