ruby - to_json give "nil is not a symbol", but object is not nil -
i'm receiving strange error here. generate random data , insert array, can see below:
@all_stores = [] 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()
after that, call method save array of items file, following:
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 @ 'public/data.json'" end
running that, receive following error:
rake aborted! nil not 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 running task --trace)
finally, if change items_to_json
method 1 below, works.
def items_to_json() file.open("public/data.json", "w") { |f| f.write(@all_stores.inspect)} puts "saved @ 'public/data.json'" end
so have in 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">]
if @all_stores
isn't nil, why method giving me that? how can solve error?
Comments
Post a Comment