after_save回调:TypeError:Bid无法强制转换为Fixnum

时间:2013-09-24 22:05:46

标签: ruby-on-rails

我在此出价模式中有一个after_save回调,但我一直收到此错误:/ items / 11 /出价中的TypeError无法强制转换为Fixnum

以下是我的出价模式:

class Bid < ActiveRecord::Base
  belongs_to :user
  belongs_to :item

  validates :amount, presence: true
  validate :check_if_highest_bid

  after_save :bid_logic


  def check_if_highest_bid
    errors.add(:amount, "You must enter a higher bid") unless self.item.price < self.amount
  end


  def bid_logic
    add_previous_bidders_bid
    update_items_current_price
    subtract_current_users_price
  end


  def add_previous_bidders_bid
      price = self.item.price
      bid = Bid.find_by_amount(price)
      unless bid.nil?
      user = User.find(bid.user_id)
      user.budget += bid
      user.save
      end

  end

  def update_items_current_price
    self.item.price = self.amount
    self.item.save
  end

  def subtract_current_users_price
    user = User.find(self.user_id)
    user.budget -= self.amount
    user.save
  end

end

这个想法是在add_previous_bidders_bid方法中,当过去的用户出价时,这笔钱会返回到注册时设置的用户预算。

这是架构:

create_table "bids", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "item_id"
    t.integer  "user_id"
    t.integer  "amount"
  end

  create_table "items", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
    t.string   "title"
    t.integer  "price"
  end

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "budget"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

任何提示?

1 个答案:

答案 0 :(得分:1)

尝试user.budget += bid.amount而不是user.budget += bid - 它不知道如何处理出价......