でproduct.rb
、私は持っています:
def active_question_description_for_standard_element_and_index(standard, element, index)
active_questions_for_standard_and_element(standard, element)[index-1].try(:description)
end
def active_questions_for_standard_and_element(standard, element)
Rails.cache.fetch([self, standard, element, "product_active_questions_for_standard_and_element"]) {
questions_for_standard_and_element(standard, element).select{|q| q.active}}
end
このactive_question_description_for_standard_element_and_index
メソッドは私にこのエラーを与えています: NoMethodError: undefined method 'description' for 0:Fixnum
.
したがって、self.touch
キャッシュをバイパスするために から始めて、 と入力しactive_questions_for_standard_and_element(standard, element)
ます。これは を返します0
。
「うーん、それは空の配列であっても、配列を返すことを意味しています。Fixnum を返すことを意図していません。」
を試してみると、期待どおりにquestions_for_standard_and_element(standard, element).select{|q| q.active}
が返されます。[]
では、なぜ Rails は に変換[]
されるの0
でしょうか? そして、どうすればそれを止めることができますか?
アップデート
メソッドから削除するとすべてが機能するため、問題はRails.cacheに関係しているようです。[]
ただし、キャッシュへの書き込みは問題なく機能するため、これまでのところ問題が何であるかはわかりません。0
再度読み戻しても変換されません。
[1] pry(main)> Rails.cache.write('foo', [])
Cache write: foo
Dalli::Server#connect 127.0.0.1:11211
=> true
[2] pry(main)> Rails.cache.read('foo')
Cache read: foo
=> []
更新 2: 答えが見つかりました
別の方法は、同じキャッシュ キーへの書き込みでした。Rails.cache で問題を抱えている他の人へのプロンプトとして、これを聞いたままにしておくと、これは確かにデバッグでチェックする価値のあるものです。