Ruby新手:未定义的方法`with_indifferent_access’

前端之家收集整理的这篇文章主要介绍了Ruby新手:未定义的方法`with_indifferent_access’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是一个新的 Ruby程序员,我的同事帮助我开始写下面的代码,在他的环境中运行良好.但是,当我尝试在我自己的环境中运行它时,我会发现以下错误:#< Hash:0x1012392c0>的未定义方法’with_indifferent_access’. (NoMethodError)

有问题的方法代码中出现两次:

  1. require 'rubygems'
  2.  
  3. gem 'activerecord'
  4. gem 'activesupport'
  5. gem 'sailthru-client'
  6.  
  7. require 'active_support'
  8. require 'active_record'
  9. require 'sailthru'
  10.  
  11. # Setup our Sailthru object using our production Sailthru account information
  12. sailthru = Sailthru::SailthruClient.new()
  13.  
  14. # Read database information from the database.yml file
  15. CONFIG = YAML.load_file(File.join(File.dirname(__FILE__),'database.yml')).with_indifferent_access
  16.  
  17. # Create a simple way for us to iterate through all publications
  18. class Publication < ActiveRecord::Base
  19. establish_connection CONFIG[:production]
  20. set_table_name 'publications'
  21. end
  22.  
  23. # Create a simple way for us to store data locally
  24. class CurrentReport < ActiveRecord::Base
  25. establish_connection CONFIG[:development]
  26. set_table_name 'current_reports'
  27. end
  28.  
  29. class MonthlyReport < ActiveRecord::Base
  30. establish_connection CONFIG[:development]
  31. set_table_name 'monthly_reports'
  32. end
  33.  
  34. types = %w[Daily Weekly]
  35.  
  36. Publication.find_each(:select => :id) do |publication|
  37. begin
  38. types.each do |newsletter_type|
  39. # Get the stats for each mailing list
  40. response = sailthru.stats_list("#{newsletter_type} Newsletter - #{publication.id}").with_indifferent_access
  41.  
  42. if response[:error]
  43. puts "Sailthru Error #{response[:error]}: #{response[:errormsg]}"
  44. else
  45. # Try to find an existing record for this newsletter
  46. daily = CurrentReport.find_or_initialize_by_list(response[:list])
  47. # and update it with the information from the response (minus the monthly signup info)
  48. puts "Updating #{newsletter_type} Newsletter - #{publication.id} ..."
  49. daily.update_attributes(response.reject { |k,v| k =~ /signup/ })
  50.  
  51. # Iterate through the monthly signup info
  52. response['signup_month'].each do |k,v|
  53. # And try to save it
  54. monthly = MonthlyReport.find_or_initialize_by_list_and_month(response[:list],k)
  55. # Only save new months,because the old months never change
  56. if monthly.new_record?
  57. monthly.update_attributes(v)
  58. puts "\tAdding #{v[:month]} to #{response[:list]} ..."
  59. end
  60. end
  61. end
  62. end
  63. # rescue NoMethodError => e
  64. # puts "Got a NoMethodError for some reason. Here's the publication: #{publication.inspect}\n\nHere's the types array: #{types}"
  65. end
  66. end

我尝试过不同版本的ruby,比如ruby-1.8.7,但无济于事.我对如何解决这个问题感到茫然.我知道这个方法存在于某处,因为我看到它有效.我对接下来要尝试的任何建议持开放态度.

解决方法

尝试使用:
  1. require 'active_support/core_ext/hash'

这是什么实际上将with_indifferent_access方法添加到普通的Hash类.

猜你在找的Ruby相关文章