【问题标题】:Can't convert Range in to Integer (Ruby on Rails)无法将 Range 转换为 Integer (Ruby on Rails)
【发布时间】:2012-06-18 17:31:51
【问题描述】:

我正在使用 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

它适用于 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

【问题讨论】:

    标签: ruby ruby-on-rails-3 heroku range


    【解决方案1】:

    您的生产 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 方法应该可以正常工作。

    【讨论】:

    • 有趣的是,rand() 函数引用并没有提到它完全接受范围。ruby-doc.org/core-1.9.3/Kernel.html#method-i-rand
    • 如果您阅读该描述中的第一行,它会说“如果 max 是 Range,则返回一个伪随机数,其中 range.member(number) == true。”
    • 感谢您的回复。即使我不知道如何知道生产服务器上的版本,你如何在 heroku 上安装 ruby​​。我以为它已经安装在heroku上。对不起,我正在学习ruby。
    • 我在 windows 上使用 Git
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多