无法将Range转换为Integer(Ruby on Rails)

前端之家收集整理的这篇文章主要介绍了无法将Range转换为Integer(Ruby on Rails)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 Ruby(1.9.3)和Rails(3.2.2).我有任务文件,其中包含要填充到我的数据库的一堆虚假数据.

以下是我认为导致问题的部分任务

  1. #Create random Tender and populate the db
  2. 20.times do |n|
  3. title = "#{Faker::Company.bs()} tender "
  4. company_name = Faker::Company.name
  5. opening_date=Time.at(rand * Time.now.to_i)
  6. closing_date=Time.at(opening_date + ( 8*7*24*60*60)) #add 8 weeks to the deadline
  7. bid_amount= rand(10000..100000)
  8. description=Faker::Lorem.paragraph(sentence_count = 3)
  9.  
  10.  
  11. Tender.create!(title: title,company_name: company_name,opening_date: opening_date,closing_date: closing_date,bid_amount: bid_amount,description: description )
  12. end

它适用于dev,但只有上述部分不在生产数据库上执行.我在dev上使用gem’sqlite3′,’1.3.5′.和

生产中的宝石’pg’,’0.12.2′(heroku)

当我跑

  1. git push heroku
  2. $heroku pg:reset SHARED_DATABASE --confirm myapp
  3. $heroku run rake db:migrate
  4. $heroku run rake db:populate
  5.  
  6. db:populate throws an error that says **can't covert Range to Integer.**

任何想法可能是什么问题?

编辑:bid_amount的数据类型是十进制

解决方法

您的生产ruby版本不是1.9.3.大概是1.8.7
  1. $ruby -v
  2. ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
  3. $irb
  4. >> rand(10000..100000)
  5. TypeError: can't convert Range into Integer
  6. from (irb):1:in `rand'
  7. from (irb):1
  8. >> exit
  9. $rvm use 1.9.3
  10. Using /Users/chirantan/.rvm/gems/ruby-1.9.3-p0
  11. $irb
  12. 1.9.3p0 :001 > rand(10000..100000)
  13. => 37036

在生产中安装ruby 1.9.3并且rand方法应该按预期工作.

猜你在找的Ruby相关文章