在 Mongoid 中,您可以自动销毁嵌入文档并更新其父文档吗?
class Cart
include Mongoid::Document
embeds_many :cart_items
def calculate_prices
# Set some fields
end
def remove_item(item)
# what goes here?
calculate_prices
save
end
end
class CartItem
include Mongoid::Document
embedded_in :cart
end
我希望 remove_item
能够自动从购物车中删除购物车商品,并在购物车集合的一次更新
中设置一些新价格。
这可能吗?也许有一些 API 可以将嵌入的项目标记为销毁,然后保存购物车?
class Cart
include Mongoid::Document
embeds_many :cart_items
def calculate_prices
# Set some fields
end
def remove_item(item)
# what goes here?
calculate_prices
save
end
end
class CartItem
include Mongoid::Document
embedded_in :cart
end
I would like the remove_item
to atomically remove the cart item from the cart and set some new prices in one update
to the carts collection.
Is that possible? Maybe some API to mark an embedded item for destroy and then save the cart?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可能的,先生。秘密就在
accepts_nested_attributes_for
中:在视图中:
通过该视图,您可以在单个购物车
更新
中删除购物车中的商品、更新数量并重新计算价格。That is possible, sir. The secret is in
accepts_nested_attributes_for
:In the view:
With that you can delete items from cart, update quantities and recalculate prices in a one single cart
update
.