【发布时间】:2013-11-08 00:03:19
【问题描述】:
假设我有一个Foo 模型。 Foo has_many Bar。 Bar 商店value。一个Foo 将多个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
有没有一种 Rails 方法可以轻松完成上述逻辑?
【问题讨论】:
-
您是否有不接受答案的个人政策?如果是这样,那就太糟糕了。
标签: ruby-on-rails ruby activerecord