无法将Ruby on Rails中的Range转换为整数

8

我正在使用Ruby(1.9.3)和Rails(3.2.2)。我有一个包含一堆虚假数据用于填充我的数据库的任务文件。

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

#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

在开发环境中运行正常,但生产数据库上述部分未执行。我在开发环境中使用 gem 'sqlite3', '1.3.5',而在生产环境(heroku)中使用 gem 'pg', '0.12.2'

当我运行

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

1个回答

8

你的生产环境ruby版本不是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 

安装ruby 1.9.3到生产环境,rand方法应按预期工作。

1
如果你读了那个描述的第一行,它说:“如果max是Range,则返回一个伪随机数,其中range.member(number) == true。” - Chirantan
1
谢谢您的回复。即使我不知道如何在生产服务器上查看版本,您如何在Heroku上安装Ruby?我以为它已经安装在Heroku上了。抱歉,我正在学习Ruby。 - WowBow
1
我正在windows上使用Git。 - WowBow
3
无论如何感谢。http://blog.heroku.com/archives/2012/5/9/multiple_ruby_version_support_on_heroku/ - WowBow
从WowBow的链接中:使用$ gem install bundler --pre指定Heroku的Ruby 1.9.3版本,然后在gem文件中添加ruby '1.9.3'到gems之前,最后运行$ bundle - Dan Sandland
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接