2013-11-25 37 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