2
使用索引方法將應用升級到Rails 5。問題在於它沒有遞增到下一個ActiveRecord集合記錄。下面的代碼用於在Rails 4.0中工作。試着用index_by。Rails 5:ActiveRecord集合index_by
def next_question
index = campaign.quiz_questions.index self
campaign.quiz_questions[index + 1]
end
調試器
(byebug) campaign.quiz_questions.index
*** NoMethodError Exception: undefined method `index' for #<QuizQuestion::ActiveRecord_Associations_CollectionProxy:0x007f80012d71b0>
Did you mean? index_by
使用index_by
(byebug) index = campaign.quiz_questions.index_by
#<Enumerator: #<ActiveRecord::Associations::CollectionProxy [#<QuizQuestion id: 113, campaign_id: 492, message: "Where did Hullabalooza's freak show manager send H...", created_at: "2016-07-20 20:50:32", updated_at: "2016-07-20 20:50:32">]>:index_by>
指數+ 1
(byebug) index + 1
*** NoMethodError Exception: undefined method `+' for #<Enumerator:0x007fc4db445960>
nil