gpt4 book ai didi

ruby-on-rails - rails - 左移 "<<"运算符自动保存记录

转载 作者:数据小太阳 更新时间:2023-10-29 07:02:21 28 4
gpt4 key购买 nike

需要帮助理解这段代码,据我所知,我知道“<<”附加到一个集合,但在这里它正确地保存了记录,为什么不调用 .save 方法就可以呢?

#user.rb
has_many :saved_properties, through: :property_saves, source: :property

#users_controller.rb
def update
if @user.saved_properties << Property.find(params[:saved_property_id])
render plain: "Property saved"
end

最佳答案

has_manydocumentation它说:

Adds one or more objects to the collection by setting their foreign keys to the collection's primary key. Note that this operation instantly fires update SQL without waiting for the save or update call on the parent object, unless the parent object is a new record.

关于ruby-on-rails - rails - 左移 "<<"运算符自动保存记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41414604/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com