gpt4 book ai didi

ruby-on-rails - 如果 RecordNotFound 赋值

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:41 25 4
gpt4 key购买 nike

post = Post.find 666

ActiveRecord::RecordNotFound: Couldn't find Post with 'id'=666

发布 => 无

post = "Banana"
post = Post.find 666
ActiveRecord::RecordNotFound: Couldn't find Post with 'id'=666

post => “香蕉”

如果查找未返回任何内容,我想确保 postnil。如果空手而归,是否有一种优雅的方法来分配 nil?

最佳答案

这是怎么做的:

post = Post.find_by(id: 666)

关于ruby-on-rails - 如果 RecordNotFound 赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29829791/

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