Rails回滚投标交易

时间:2016-12-31 15:57:18

标签: ruby-on-rails rollback

您好我在尝试从rails控制台创建出价时收到回滚事务。这些是我的模特:

产品型号



class Product < ApplicationRecord
	belongs_to :user
	belongs_to :category
	has_many :ratings
	has_many :bids

end
&#13;
&#13;
&#13;

投标模型:

&#13;
&#13;
class Bid < ApplicationRecord
	belongs_to :products
	belongs_to :user
	
end
&#13;
&#13;
&#13;

用户模型:

&#13;
&#13;
class User < ApplicationRecord
	has_many :products
	has_many :ratings
	has_many :bids
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end
&#13;
&#13;
&#13;

这是我的架构:

&#13;
&#13;
ActiveRecord::Schema.define(version: 20161231124005) do

  create_table "bids", force: :cascade do |t|
    t.integer  "amount"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.integer  "product_id"
    t.index ["product_id"], name: "index_bids_on_product_id"
    t.index ["user_id"], name: "index_bids_on_user_id"
  end

  create_table "categories", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "products", force: :cascade do |t|
    t.string   "title"
    t.text     "description"
    t.string   "image_url"
    t.integer  "price"
    t.datetime "deadline"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "user_id"
    t.integer  "category_id"
  end

  create_table "ratings", force: :cascade do |t|
    t.integer  "rating"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.integer  "product_id"
  end

  create_table "users", force: :cascade 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,  null: false
    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",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "username"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["username"], name: "index_users_on_username", unique: true
  end

end
&#13;
&#13;
&#13;

虽然我尝试像这样创建:Bid.create(金额:500,user_id:1,product_id:6)但由于回滚事务而无法保存。

提前致谢

1 个答案:

答案 0 :(得分:1)

您发布的代码并不能提供帮助。您还应该添加日志。 在发布任何日志之前,我在控制台中尝试b = Bid.new(amount: 500, user_id: 1, product_id: 6)b.save。之后,执行b.errors并查看导致回滚的原因。

编辑:添加.save

EEDIT:对于遇到相同问题的任何人,问题在于引用Bid错误的Product模型。 使用belongs_to时,模型应该是单数,而不是复数。例如:belongs_to: apple不是belongs_to: apples