是否可以强制Rails动态查找器在找不到结果时抛出ActiveRecord::RecordNotFound异常而不是返回nil?例如,如果名称为“Nuka–Cola”的饮料不存在:@not_found=Beverage.find_by_name('Nuka–Cola')而不是拥有@not_found==nil可以吗.find_by_name('Nuka–Cola')方法调用抛出ActiveRecord::RecordNotFound异常?或者我是否必须检查nil并手动抛出异常? 最佳答案 使用bang版本。@not_found=Be
我有这个代码:Article.find([1,2,3])但数据库中只有记录1和2。我得到这个异常(exception):"ActiveRecord::RecordNotFound(Couldn'tfindallOfferswithIDs(1,2,3)(found2results,butwaslookingfor3))"有没有办法只获取现有记录而不获取异常? 最佳答案 Article.find_all_by_id([1,2,3])是要走的路! 关于ruby-on-rails-查询多条记录时
当模型无法检索记录时,我的应用程序正在将ActiveRecord::RecordNotFound传播到Controller。这是数据库查询:defself.select_intro_verseoffset=rand(IntroVerse.count)IntroVerse.select('line_one','line_two','line_three','line_four','line_five').offset(offset).first!.as_jsonendfirst!如果没有找到记录,则引发一个ActiveRecord::RecordNotFound,我可以挽救它并在我的Co
我的Controller中有这段代码,我想通过功能测试来测试这段代码。raiseActiveRecord::RecordNotFoundif@post.nil?我应该使用哪种断言方法?我使用内置的rails2.3.5测试框架。我用这段代码试过了:test"shouldreturn404ifpagedoesn'texist."doget:show,:url=>["nothing","here"]assert_response:missingend但这对我不起作用。得到这个测试输出:test_should_return_404_if_page_doesn't_exist.(PageCont
我目前正在使用gorm和ginframework.我想知道如果发生错误并且我的应用程序没有处理它会发生什么?例子:iferr:=db.Where("name=?","jinzhu").First(&user).Error;err!=nil{//errorhandling...}在上面的例子中,错误正在被处理。ifdb.Model(&user).Related(&credit_card).RecordNotFound(){//nocreditcardfounderrorhandling}在上面的下一个示例中,仅处理了RecordNotFound()错误,但是如果它抛出其他错误怎么办?会发
我目前正在使用gorm和ginframework.我想知道如果发生错误并且我的应用程序没有处理它会发生什么?例子:iferr:=db.Where("name=?","jinzhu").First(&user).Error;err!=nil{//errorhandling...}在上面的例子中,错误正在被处理。ifdb.Model(&user).Related(&credit_card).RecordNotFound(){//nocreditcardfounderrorhandling}在上面的下一个示例中,仅处理了RecordNotFound()错误,但是如果它抛出其他错误怎么办?会发
我遇到了thislibrary的问题因为即使给定的输入不在数据库中,此函数也会返回false,而实际上它应该返回true。typeUserstruct{IDuint`gorm:"primary_key"`Usernamestring`json:",omitempty"`Passwordstring`json:",omitempty"`CreatedAttime.Time`json:",omitempty"`}b,err:=db.Con()iferr!=nil{log.Panic(err)}deferdb.Close()//Wewantanemptystruct//Otherwiseit