Rails 5:ActiveRecord集合index_by [英] Rails 5: ActiveRecord collection index_by
本文介绍了Rails 5:ActiveRecord集合index_by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用索引方法将应用程序升级到Rails 5。问题是它没有增加到下一个ActiveRecord集合记录。下面的代码用于在Rails 4.0中工作。
Upgraded app to Rails 5 using the index method. The issue is that it is not incrementing to the next ActiveRecord collection record. The below code below use to work in Rails 4.0. Tried with 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
Using 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
推荐答案
进行了更改到 find_index
方法。现在可以正常使用了。
changed it to find_index
method. Now it's working
def next_question
index = campaign.quiz_questions.find_index self
campaign.quiz_questions[index + 1]
end
这篇关于Rails 5:ActiveRecord集合index_by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文