0

おそらく、この長すぎるずさんなデザイン、またはその両方に取り組んできたのでしょう。私の問題は、初期化したいモデルがあることです。オブジェクトには 52 個の属性がありますが、スキャンしたオブジェクトに応じて特定の ~25 しか設定していません。オブジェクトをスキャンすると、列が取得され、作成した hash_map と照合されます。

ハッシュマップの例

これは、スキャンされたテキストをそれぞれの属性名に一致させるだけです。

hash_map = {"Pizza."=>"pizza_pie","PastaBowl"=>"pasta_bowl","tacos"=>"hard_shell_taco","IceCream"=>"ice_cream","PopTarts"=>"pop_tart"}

私がしたいこと

menu = RestaurantMenu.new(pizza_pie => var1, pasta_bowl => var2, ...)

私の唯一の問題は、これを持っている現時点での私のコードにあります...

t.rows.each do |r|
  for i in 0..r.length-1
     #hash_map[t.combined_columns[i]] => r.[i]
     puts "#{hash_map["#{t.combined_columns[i]}"]} => #{r[i]}"
  end
end

行には必要なものがputs表示されますが、アプリでそれを適切に取得する方法がわかりません。

4

2 に答える 2

0

私は最終的に次の方法を実行しました:

attributes = Hash[]
attributes["restaurant"] = tmp_basic_info.name
attributes["menu_item"] = tmp_basic_info.item_name

t.rows.each do |r|
  for i in 0..r.length-1
     attributes["other"] = t.other_information
     attributes[hash_map[t.combined_columns[i]] = r[i]
  end
  row = ImportMenuItem.new(attributes)
  row.save
end
于 2013-07-07T18:20:41.993 に答える
0

これを修正するには、いくつかの方法があります。

hash_map = {"Pizza."=>"pizza_pie","PastaBowl"=>"pasta_bowl","tacos"=>"hard_shell_taco","IceCream"=>"ice_cream","PopTarts"=>"pop_tart"}

attributes.each do |attribute, element|
  message.send((attribute + '=').to_sym, hash_map[element])
end

またはこのように:

class Example
  attr_reader :Pizza, :PastaBowl #...

  def initialize args
    args.each do |k, v|
      instance_variable_set("@#{k}", v) unless v.nil?
    end
  end
end

詳細については、ここをクリックしてください

于 2013-07-07T06:12:01.357 に答える