gpt4 book ai didi

ruby-on-rails - Ruby on rails - 嵌套属性 : How to do a find or create of the nested model

转载 作者:行者123 更新时间:2023-12-04 15:49:08 24 4
gpt4 key购买 nike

我有一个 Bill嵌套模型 Customer模型。Customer模型有一个带有唯一性验证的电话号码。
在创建账单时,我想根据电话号码获取现有记录,或者如果不存在则创建一个新记录。
我应该怎么做 RESTful道路?

最佳答案

您将使用 find_or_create_by 方法,在您的情况下它看起来像这样:

fetchedRecord = Bill.find_or_create_by_phone_number(customer.phone_number)

关于ruby-on-rails - Ruby on rails - 嵌套属性 : How to do a find or create of the nested model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2014402/

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