to_json给出“nil不是符号”,但是对象不是nil

时间:2014-01-31 21:13:50

标签: ruby json serialization rake task

我在这里收到一个奇怪的错误。我生成一些随机数据并插入到数组中,如下所示:

@all_stores = []
for i in 0..10
  item = Item.new(
      :name => generate_random_product(), 
      :description => generate_random_product(),
      :url => generate_random_product(),
      :image_url => generate_random_product(),
      :type => "Goods",
      :category => "Electronics"
    )
  @all_stores << item
end

write_to_file()

之后,我调用一个方法将这个项目数组保存到文件中,如下所示:

def write_to_file()
   items_to_json()
end

def items_to_json()
   File.open("public/data.json", "w") { |f| f.write(@all_stores.to_json)}
   puts "Saved at 'public/data.json'"
end

运行它,我收到以下错误:

rake aborted!
nil is not a symbol
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activemodel-4.0.2/lib/active_model/serialization.rb:108:in `block in serializable_hash'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activemodel-4.0.2/lib/active_model/serialization.rb:108:in `each'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activemodel-4.0.2/lib/active_model/serialization.rb:108:in `serializable_hash'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activerecord-4.0.2/lib/active_record/serialization.rb:17:in `serializable_hash'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activemodel-4.0.2/lib/active_model/serializers/json.rb:99:in `as_json'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.0.2/lib/active_support/json/encoding.rb:58:in `block in as_json'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.0.2/lib/active_support/json/encoding.rb:81:in `check_for_circular_references'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.0.2/lib/active_support/json/encoding.rb:57:in `as_json'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.0.2/lib/active_support/json/encoding.rb:270:in `block in as_json'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.0.2/lib/active_support/json/encoding.rb:270:in `map'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.0.2/lib/active_support/json/encoding.rb:270:in `as_json'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.0.2/lib/active_support/json/encoding.rb:50:in `block in encode'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.0.2/lib/active_support/json/encoding.rb:81:in `check_for_circular_references'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.0.2/lib/active_support/json/encoding.rb:49:in `encode'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.0.2/lib/active_support/json/encoding.rb:34:in `encode'
/home/paladini/.rvm/gems/ruby-2.0.0-p353@global/gems/activesupport-4.0.2/lib/active_support/core_ext/object/to_json.rb:16:in `to_json'
/home/paladini/Documents/Projetos/bittle/parse_usebitcoins/lib/tasks/data.rake:402:in `block in to_json'
/home/paladini/Documents/Projetos/bittle/parse_usebitcoins/lib/tasks/data.rake:402:in `open'
/home/paladini/Documents/Projetos/bittle/parse_usebitcoins/lib/tasks/data.rake:402:in `to_json'
/home/paladini/Documents/Projetos/bittle/parse_usebitcoins/lib/tasks/data.rake:383:in `write_to_file'
/home/paladini/Documents/Projetos/bittle/parse_usebitcoins/lib/tasks/data.rake:32:in `block (2 levels) in <top (required)>'
Tasks: TOP => data:fast_test
(See full trace by running task with --trace)

最后,如果我将items_to_json方法更改为下面的方法,则可以正常工作。

def items_to_json()
   File.open("public/data.json", "w") { |f| f.write(@all_stores.inspect)}
   puts "Saved at 'public/data.json'"
end

所以我在data.json

[#<Item name: "iRyqyHaMYtGrHTe", description: "SnCXDCMcasmIdrL", url: "GoEyRKsxfGeNuJC", type: "Goods", category: "Electronics", image_url: "fgjcXCTCcpLbLuq">, #<Item name: "DEhnKIsxdMMeMpE", description: "YFDybXOekEMnzGE", url: "CwcMZTcuDfraPFd", type: "Goods", category: "Electronics", image_url: "lYzoirDTGObjdip">, #<Item name: "kxVosdEszPnRAuT", description: "XWkLdKwXoByeibN", url: "psNbXlgqAquFEnL", type: "Goods", category: "Electronics", image_url: "hSVirvVIEvXjpNc">, #<Item name: "XXboCYXaiOFuZlm", description: "SXiWoebtSgBJQiF", url: "ZNnTfdeqDoChRdW", type: "Goods", category: "Electronics", image_url: "QIWxWlqaosTtcYK">, #<Item name: "kDHyIJvaZsCgbPt", description: "DoRHJplzfubidlh", url: "NmLqFabhxyqDLci", type: "Goods", category: "Electronics", image_url: "uMqzgLOywOQIVpg">, #<Item name: "QDLaYGbTwnkqyWt", description: "HMzKmUxoLdFwEcL", url: "cTZzHSVQtAPWeqL", type: "Goods", category: "Electronics", image_url: "CcbMFwTaMUNSImt">, #<Item name: "oLHowKZBlJdBDCo", description: "yDeXEYSiusbUlsN", url: "gXRDgTnQMTkYhuD", type: "Goods", category: "Electronics", image_url: "vYOiHDlgbVSdwgM">, #<Item name: "biwsysSbECbktdb", description: "eLFfUBTAPFUYEcl", url: "hDDfCFhwiVmbtqR", type: "Goods", category: "Electronics", image_url: "HrpWUYFgpGYThSm">, #<Item name: "BsHGxVsjlXxPPvQ", description: "oHkkqmFUqcCzGAc", url: "jetgofPYmnOARzy", type: "Goods", category: "Electronics", image_url: "SZWwCAKfIBMrKQi">, #<Item name: "UCPqMLIVhQnGBYR", description: "qwuApfmBHEJPuin", url: "QEhSCKnIJciBSnd", type: "Goods", category: "Electronics", image_url: "SWIlGqPRHABZIaX">, #<Item name: "TLpeNBzZcrEClRu", description: "olABsfHfNiIcaok", url: "fsiYMWbImrRNfnt", type: "Goods", category: "Electronics", image_url: "RgzZaFxFJgMangz">]

如果@all_stores不是零,为什么这种方法给我这个?我该如何解决这个错误?

0 个答案:

没有答案