2012-06-15 60 views
7

我正在使用Ruby(1.9.3)和Rails(3.2.2)。我有任务文件,其中包含一些假数据将填充到我的数据库。无法将范围转换为整数(Ruby on Rails)

这里是我认为是造成问题

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


     Tender.create!(title: title, 
        company_name: company_name, 
        opening_date: opening_date, 
        closing_date: closing_date, 
      bid_amount: bid_amount , 
      bid_amount: bid_amount , 
      description: description) 
    end 

它正常工作与开发,但只有在生产数据库中不执行上述部分任务的某些部分。我在dev上使用gem 'sqlite3', '1.3.5'。并在生产

gem 'pg', '0.12.2'(Heroku的)

当我运行

git push heroku 
$ heroku pg:reset SHARED_DATABASE --confirm myapp 
$ heroku run rake db:migrate 
$ heroku run rake db:populate 

db:populate throws an error that says **can't covert Range to Integer.** 

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

编辑: BID_AMOUNT的数据类型是decimal

回答

7

您的生产红宝石版本不是1.9.3。它可能是1.8.7

$ ruby -v 
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0] 
$ irb 
>> rand(10000..100000) 
TypeError: can't convert Range into Integer 
    from (irb):1:in `rand' 
    from (irb):1 
>> exit 
$ rvm use 1.9.3 
Using /Users/chirantan/.rvm/gems/ruby-1.9.3-p0 
$ irb 
1.9.3p0 :001 > rand(10000..100000) 
=> 37036 

在生产上安装红宝石1.9.3和rand方法应该按预期工作。

+0

有趣的是,rand()函数的引用没有提及它接受范围.. http://ruby-doc.org/core-1.9.3/Kernel.html#method-i-rand – MBHNYC

+1

如果您在该描述中读取第一行,它说:“如果max是Range,则返回一个伪随机数,其中range.member(number)== true。” – Chirantan

+0

对于1.8.7 http://ruby-doc.org/core-1.8.7/Kernel.html#method-i-和 – Chirantan