这是我的表:
create_table "books", force: :cascade do |t|
t.string "book_name"
t.integer "cart_unit"
t.integer "product_id"
t.integer "pages"
t.integer "stock"
t.text "synopsis"
t.integer "mrp"
t.string "author"
t.string "publisher"
t.integer "rating"
t.string "genre_name"
t.integer "book_id"
t.integer "genre_id"
t.integer "discount"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
这是我的模型:
class Book < ApplicationRecord
self.primary_key = 'book_id'
belongs_to:Product
end
这是rails控制台中发生的事情:
irb(main):044:0> book = Book.new(book_name:"The Girl on the Train", cart_unit:0, product_id:0001, pages:395, stock:50, synopsis:"Commuter Rachel Watson catches daily glimpses of a seemingly perfect couple, Scott and Megan, from the window of her train. One day, Watson witnesses something shocking unfold in the backyard of the strangers' home. Rachel tells the authorities what she thinks she saw after learning that Megan is now missing and feared dead. Unable to trust her own memory, the troubled woman begins her own investigation, while police suspect that Rachel may have crossed a dangerous line.", mrp:500, author:"Paula Hawkins", publisher:"Riverhead Books",rating:8, genre_name:"Thriller", genre_id:1, book_id:1001, discount:0)
=> #<Book id: 1001, book_name: "The Girl on the Train", cart_unit: 0, product_id: 1, pages: 395, stock: 50, synopsis: "Commuter Rachel Watson catches daily glimpses of a...", mrp: 500, author: "Paula Hawkins", publisher: "Riverhead Books", rating: 8, genre_name: "Thriller", book_id: 1001, genre_id: 1, discount: 0, created_at: nil, updated_at: nil>
irb(main):045:0> book.save
(0.1ms) begin transaction
(0.1ms) rollback transaction
=> false
为什么会这样?我不明白为什么交易会回滚。 编辑:运行'book.save!'
book.save!
(0.1ms) begin transaction
(0.1ms) rollback transaction
ActiveRecord::RecordInvalid: Validation failed: Product must exist
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/validations.rb:78:in `raise_validation_error'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/validations.rb:50:in `save!'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/transactions.rb:324:in `block in save!'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/transactions.rb:211:in `transaction'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/transactions.rb:324:in `save!'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activerecord-5.0.1/lib/active_record/suppressor.rb:45:in `save!'
from (irb):53
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in `start'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `<top (required)>'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `block in load'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/gauri/.rbenv/versions/2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
这是产品表:
create_table "products", force: :cascade do |t|
t.integer "product_id"
t.string "product_name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
这是产品表的模型
class Product < ApplicationRecord
self.primary_key = 'product_id'
end
答案 0 :(得分:0)
这样做。
book.errors
它会为您提供详细信息。 或者,您可以先执行此操作
book = Book.new(params)
然后这个
book.save!
它会引发运行时错误并显示错误。
查看上一条评论后,您的产品不存在。首先创建您在Book记录中传递的ID的产品。
答案 1 :(得分:0)
您必须使用Rails 5,默认情况下需要belongs_to
。如果您希望product_id是可选的,
class Book < ApplicationRecord
self.primary_key = 'book_id'
belongs_to: Product, optional: true
end