Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 没有使用哈希将符号隐式转换为整数错误_Ruby_Ruby On Rails 4_Ruby Hash - Fatal编程技术网

Ruby 没有使用哈希将符号隐式转换为整数错误

Ruby 没有使用哈希将符号隐式转换为整数错误,ruby,ruby-on-rails-4,ruby-hash,Ruby,Ruby On Rails 4,Ruby Hash,我得到错误“没有将符号隐式转换为整数” 这是我的密码: # == Schema Information # # Table name: my_payments # # id :integer not null, primary key # email :string # ip :string # status :string # fee :dec

我得到错误“没有将符号隐式转换为整数”

这是我的密码:

# == Schema Information
#
# Table name: my_payments
#
#  id               :integer          not null, primary key
#  email            :string
#  ip               :string
#  status           :string
#  fee              :decimal(6, 2)
#  paypal_id        :string
#  total            :decimal(8, 2)
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#  shopping_cart_id :integer
#

class MyPayment < ActiveRecord::Base
    belongs_to :shopping_cart
    include AASM 

    aasm column: "status" do
        state :created, initial: true
        state :payed
        state :failed

        event :pay do
            after do
                shopping_cart.pay!
                self.update_stock_products()
            end
            transitions from: :created, to: :payed
        end
    end

    def update_stock_products       
        self.shopping_cart.in_shopping_carts.map { |i_sh|
            product = i_sh.product
            self_product = Product.where(id: product.id)
            num = self_product[:stock]
            res = num - i_sh.num_products 
            Product.update(product.id,stock: res)                   
        }       
    end
end

self_产品被视为一个数组(或类似数组的东西),因此它需要一个数字索引,而不是一个符号,正如您对散列或活动记录实例所期望的那样

问题在于:

self_product = Product.where(id: product.id)
这将返回一个ActiveRecord关系对象。对其使用[]运算符将运行查询并返回第n项

您可能需要以下内容:

num = Product.find(product.id).stock
但是,如果您有购物车项目与
product
设置的关联权限,则不需要这样做。你应该能够做到:

num = product.stock

self_产品被视为一个数组(或类似数组的东西),因此它需要一个数字索引,而不是一个符号,正如您对散列或活动记录实例所期望的那样

问题在于:

self_product = Product.where(id: product.id)
这将返回一个ActiveRecord关系对象。对其使用[]运算符将运行查询并返回第n项

您可能需要以下内容:

num = Product.find(product.id).stock
但是,如果您有购物车项目与
product
设置的关联权限,则不需要这样做。你应该能够做到:

num = product.stock

非常感谢,与num=product.stock合用!我把啤酒送到哪里去了?哈哈,谢谢!!!我身上有一杯庆祝啤酒:D awesome Sr。非常感谢,与num=product.stock合用!我把啤酒送到哪里去了?哈哈,谢谢!!!请给我来杯庆祝啤酒