1
我在此出價模型中有after_save回調,但我不斷收到此錯誤:TypeError at/items/11/bids出價不能強制轉換爲Fixnumafter_save回調: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
任何提示嗎?
我傻的,當然! – Dileet