Ruby on rails 4 从ActiveRecord解救::范围内未找到RecordNotFound

Ruby on rails 4 从ActiveRecord解救::范围内未找到RecordNotFound,ruby-on-rails-4,Ruby On Rails 4,想知道是否有可能从活动记录中的作用域中进行救援 我的模型 class Question < ActiveRecord::Base scope :active, -> {where("is_active = 't'")} validates_inclusion_of :is_active, :in => [true, false] end 我尝试了从ActiveRecord::RecordNotFound中解救\u,但出现了::no\u record\u错误,但似乎没有

想知道是否有可能从活动记录中的作用域中进行救援

我的模型

class Question < ActiveRecord::Base
  scope :active, -> {where("is_active = 't'")}
  validates_inclusion_of :is_active, :in => [true, false]
end
我尝试了
从ActiveRecord::RecordNotFound中解救\u,但出现了::no\u record\u错误
,但似乎没有效果


当没有返回活动问题时,如何捕获/拯救异常?(我想呈现另一个视图)。非常感谢

您的范围永远不会引发异常。使用where not
find
(这会引发en异常)

def get_active_question
  begin
    @question = Question.active.first
  end
end
def get_active_question
  @question = Question.active.first

  if @question
    # you have a question 
    render 'one_view'
  else 
    # no active questions
    render 'another_view'
  end
end