【问题标题】:Record entry error in rails在rails中记录输入错误
【发布时间】:2017-04-25 14:41:27
【问题描述】:

这是我的桌子:

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

【问题讨论】:

  • 是否有任何验证?
  • 使用book.valid?检查错误。
  • @Gerry book.valid? => 错误
  • 尝试book.save!,然后粘贴错误
  • @seaify-Freelancer 这就是我在问题中所写的

标签: ruby-on-rails database sqlite transactions


【解决方案1】:

这样做。

book.errors

它将为您提供详细信息。 或者,您可以先这样做

book = Book.new(params)

然后这个

book.save!

它会引发运行时错误并显示错误。

查看您的最后一条评论后,您的产品不存在。首先创建您在 Book 记录中传递的 id 的 Product。

【讨论】:

  • 我制作了产品,但它仍然显示产品不存在。是否有关于我应该如何编写 product_id 的特殊符号?
  • 在您将这个验证添加到您的图书模型validates :product, presence: true 之前,它应该可以工作。很少有人建议您应该将您的列g product_idbook_id 重命名为product_codebook_code 之类的名称。因为默认情况下,rails 使用 Table 的唯一 ID 创建外键。重命名列名后,您应该在本书模型中明确提供外键,如 belongs_to :product, foreign_key: 'product_code' 。它应该可以工作。
  • 进行此类更改后不要忘记退出 Rails 控制台。
【解决方案2】:

您必须使用 Rails 5,默认情况下需要 belongs_to。如果您希望 product_id 是可选的,

class Book < ApplicationRecord
    self.primary_key = 'book_id'
    belongs_to: Product, optional: true
end

【讨论】:

  • 还是说产品必须存在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 2022-01-15
  • 1970-01-01
  • 2013-12-04
  • 2013-01-04
  • 1970-01-01
相关资源
最近更新 更多