0

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 で問題を抱えている他の人へのプロンプトとして、これを聞いたままにしておくと、これは確かにデバッグでチェックする価値のあるものです。

4

1 に答える 1

0

active_questions_for_standard_and_element確かに配列を返すかもしれませんが、それに適用[index-1]することで、その配列から要素を取得しています。配列がサブ配列を含むことを意図していない限り、結果が配列になることを期待すべきではありませんactive_questions_for_standard_and_element(...)[index-1]

于 2013-08-28T01:17:18.470 に答える