灵活的网页开发与轨道4

我需要你的帮助。
我的Ruby on Rails教程(带rails 4的敏捷web开发,块g)给了我一个错误。 我是Ruby新手,不知道如何解决这个问题。

来源

class CombineItemsInCart < ActiveRecord::Migration
    def up

      Cart.all.each do |cart|
        # how many goods are in the cart?
        sums = cart.line_items.group(:product_id).sum(:quantity)
        sums.each do |product_id; quantity|
          if quantity > 1
            # remove lines
            cart.line_items.where(:product_id product_id).delete_all

            # replace with a one line
            item = cart.line_items.build(product_id: product_id)
            item.quantity = quantity
            item.save!
          end
       end
    end
  end
end

错误是:

迁移正在进行中; 运行'rake db:migrate RAILS_ENV = development'来解决这个问题。

但是当我试图运行rake db:migrate时
我在终端收到这个错误:

rake db:migrate RAILS_ENV =开发

耙子中止了! SyntaxError:/home/yury/Desktop/rails/depot_g/db/migrate/20170401195439_combine_items_in_cart.rb:10:语法错误,意外的tIDENTIFIER,期待')'cart.line_items.where(:product_id product_id).delete_all ^ / home / yury /.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in require' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in block中require' require' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in 4.0。 0 / lib / active_support / dependencies.rb:213:in load_dependency' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in / load_dependency' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in / load_dependency' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in / load_dependency' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in / load_dependency' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in / load_dependency' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in / load_dependency' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in /lib/active_support/dependencies.rb: load_dependency' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in require'/home/ load_migration' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:714:in gems/ load_migration' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:714:in gems/ load_migration' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:714:in active_record/ load_migration' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:714:in load_dependency' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in load_migration' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:714:in migration'/home/yury/.rvm/gems/ruby-2.0.0-p648/宝石/了activerecord-4.0.0 / LIB / active_record / migration.rb:70 8:在disable_ddl_transaction' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:1012:in : disable_ddl_transaction' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:1012:in :in use_transaction?' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:1004:in ddl_transaction' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:958:in yury/ ddl_transaction' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:958:in execute_migration_in_transaction' ddl_transaction' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:958:in 4.0.0 / lib / active_record / migration.rb:920: block in migrate' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in中的block in migrate' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in每个'/home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in migrate' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/migration.rb:764:in '/home/yury/.rvm/gems/ruby-2.0。 0-p648 / gems / activerecord-4.0.0 / lib / active_record / migration.rb:742:in migrate' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:42:in /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0 migrate' /home/yury/.rvm/gems/ruby-2.0.0-p648/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:42:in levels)in'/home/yury/.rvm/gems/ruby-2.0.0-p648/gems/rake-12.0.0/exe/rake :27:在<top (required)>' /home/yury/.rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:15:in <top (required)>' /home/yury/.rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:15:in yury/.rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:15: <top (required)>' /home/yury/.rvm/gems/ruby-2.0.0-p648/bin/ruby_executable_hooks:15:in eval'/home/yury/.rvm/gems/ruby-2.0.0-p648/bin / ruby​​_executable_hooks:15:在''任务:TOP => db:migrate(通过使用--trace运行任务来查看完整跟踪)

系统:Ubuntu 16.04 64位。 我检查了几次代码,它和本书中的一样。

那么有人能帮助我,并告诉我做错了什么吗? 我该如何解决它? 谢谢!


正如它所说的“ syntax error, unexpected tIDENTIFIER, expecting ')' ”,那么在你的代码语法中有一个错字,并且在你的sums.each块中,改变

sums.each do |product_id; quantity|

对于

sums.each do |product_id, quantity|

而在你if从这句话

cart.line_items.where(:product_id product_id)

对此

cart.line_items.where(product_id: product_id)

检查书中的原始代码:

# rails40/depot_g/db/migrate/20121130000005_combine_items_in_cart.rb
def up
  # replace multiple items for a single product in a cart with a single item
  Cart.all.each do |cart|
    # count the number of each product in the cart
    sums = cart.line_items.group(:product_id).sum(:quantity)
    sums.each do |product_id, quantity|
      if quantity > 1
        # remove individual items
        cart.line_items.where(product_id: product_id).delete_all
        # replace with a single item
        item = cart.line_items.build(product_id: product_id)
        item.quantity = quantity
        item.save!
      end
    end
  end
end
链接地址: http://www.djcxy.com/p/49949.html

上一篇: g agile web development with rails 4

下一篇: Add reference in my migration error