私は、多くのコンポーネントを持つ製品のモデルを構築しようとしています。一部のコンポーネントはオプションで、ユーザーが有効にするかどうかの選択に依存します。 私は2つのモデルを持っています。一つは設定、もう一つは(その設定の)要素です。ハッシュ配列の要素を見つける
私は配列のすべての要素を持ってきて、デフォルトで表示される配列の別の配列を作成します。 しかし、次のコードを書くと、両方のオブジェクトがハッシュの配列であるにもかかわらずエラーが表示されます。することができますよう
irb(main):252:0* @all = Configuration.find(1).elements
=> [#<Element id: 1, name: "elem1", quantity: 1, position: 1, subposition: nil, created_at: nil, updated_at: nil, configuration_id: 1>, #<Element id: 2, name: "elem2", quantity: 2, position: 2, subposition: 1, created_at: nil, updated_at: nil, configuration_id: 1>, #<Element id: 3, name: "elem3", quantity: 3, position: 2, subposition: 2, created_at: nil, updated_at: nil, configuration_id: 1>, #<Element id: 4, name: "elem4", quantity: 4, position: 3, subposition: nil, created_at: nil, updated_at: nil, configuration_id: 1>]
は、その後、私は、これまでのところは良いsubpositionのゼロまたは1
irb(main):253:0> @default = @all.where(:subposition=>nil).concat(@all.where(:subposition=>1))
=> [#<Element id: 1, name: "elem1", quantity: 1, position: 1, subposition: nil, created_at: nil, updated_at: nil, configuration_id: 1>, #<Element id: 4, name: "elem4", quantity: 4, position: 3, subposition: nil, created_at: nil, updated_at: nil, configuration_id: 1>, #<Element id: 2, name: "elem2", quantity: 2, position: 2, subposition: 1, created_at: nil, updated_at: nil, configuration_id: 1>]
を持っているもののみをするフィルタ:
は、だから私はすべての要素の私の最初の配列を持ってElem3が@defaultに表示されていないのは、要求を満たしていないからです。
特定の操作を実行する必要があるため、配列を再生しようとすると問題が発生します。
irb(main):257:0> @all.where(:position =>1)
=> [#<Element id: 1, name: "elem1", quantity: 1, position: 1, subposition: nil, created_at: nil, updated_at: nil, configuration_id: 1>]
しかし@defaultで同じ操作が失敗し、
irb(main):258:0> @default.where(:position =>1)
NoMethodError: undefined method `where' for #<Array:0x2641660>
は今、彼らはハッシュの両方の配列をしていると同じように見え、なぜ同じ方法は、第2の場合には失敗していますか?
おかげであなたの助けのためにたくさん!