2013-11-25 40 views
0

想知道是否有可能从一个范围,活动记录抢救救助范围

我的模型

class Question < ActiveRecord::Base 
    scope :active, -> {where("is_active = 't'")} 
    validates_inclusion_of :is_active, :in => [true, false] 
end 

在控制器中,我有可能会返回一个活跃的功能问题(如果有的话)

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

我试图与rescue_from ActiveRecord::RecordNotFound, with: :no_record_error,但这似乎没有影响

如何在未返回活动问题时捕捉/解救异常? (我想提出另一种观点)。非常感谢

回答

0

您的范围绝不会引发异常。你用的地方不是find(这可能会引起例外)

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