ActiveRecord :: RecordInvalid:验证失败:技术组合必须存在

时间:2018-12-03 02:44:16

标签: ruby-on-rails

我正在尝试为具有投资组合的技术保存嵌套属性,我有以下代码: Portfolio.rb(模型)

class Portfolio < ApplicationRecord
  has_many :technologies
  # do not accept the insertion if name is blank
  accepts_nested_attributes_for :technologies,
                                 reject_if: lambda{ |attrs| attrs['name'].blank? }
  validates_presence_of :title, :body, :main_image, :thumb_image
  include Placeholder
  validates_presence_of :title, :body, :main_image, :thumb_image

  # class method - custom scope
  def self.angulars
    where(subtitle: "Angular")
  end 

  # lambda - custom scope
  scope :ruby_on_rails_p_items, -> { where(subtitle: "Ruby on Rails") }

  # callbackss
  after_initialize :set_defaults

  def set_defaults
    self.main_image ||= Placeholder.image_generator(height:'600', width:'400')
    self.thumb_image ||= Placeholder.image_generator(height:'350', width:'200')
  end 
end

technology.rb模型:

class Technology < ApplicationRecord
  belongs_to :portfolio
end

现在,当我进入Rails控制台并尝试插入多个属性时:

Portfolio.create!(title: "Web App", subtitle: "asadasd", body: "sadsadas", technologies_attributes: [{name: "Ruby"}, {name: "Rails"}, {name: "Angula"}, {name: "Ionic"}])

它给了我这个错误:

ActiveRecord::RecordInvalid: Validation failed: Technologies portfolio must exist
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/validations.rb:78:in `raise_validation_error'

知道我在这里想念什么吗?

技术和产品组合的架构:

  create_table "portfolios", force: :cascade do |t|
    t.string   "title"
    t.string   "subtitle"
    t.text     "body"
    t.text     "main_image"
    t.text     "thumb_image"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "technologies", force: :cascade do |t|
    t.string   "name"
    t.integer  "portfolio_id"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
    t.index ["portfolio_id"], name: "index_technologies_on_portfolio_id", using: :btree
  end

完整的错误消息:

ActiveRecord::RecordInvalid: Validation failed: Technologies portfolio must exist
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/validations.rb:78:in `raise_validation_error'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/validations.rb:50:in `save!'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:324:in `block in save!'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:211:in `transaction'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/transactions.rb:324:in `save!'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/suppressor.rb:45:in `save!'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.7/lib/active_record/persistence.rb:51:in `create!'
    from (irb):3
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/railties-5.0.7/lib/rails/commands/console.rb:65:in `start'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/railties-5.0.7/lib/rails/commands/console_helper.rb:9:in `start'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/railties-5.0.7/lib/rails/commands.rb:18:in `<top (required)>'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:293:in `require'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:293:in `block in require'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:293:in `require'
    from /Users/mac/Desktop/DevCampPortfolio/bin/rails:9:in `<top (required)>'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `load'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `block in load'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/mac/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `load'
    from /Users/mac/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/mac/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'
2.3.1 :004 > Portfolio.create!(title: "Web App", subtitle: "asadasd", body: "sadsadas", main_image: 'some value', thumb_image: 'some image')

2 个答案:

答案 0 :(得分:1)

您已经对Portfolio进行了验证

validates_presence_of :title, :body, :main_image, :thumb_image

您只通过:title, :subtile, :body,而不通过:main_image, :thumb_image

Portfolio.create!(title: "Web App", subtitle: "asadasd", body: "sadsadas", technologies_attributes: [{name: "Ruby"}, {name: "Rails"}, {name: "Angula"}, {name: "Ionic"}])

以上代码预期可以成功保存投资组合,因此可以在Technology中使用Portfolio_id,但是在这里没有发生,您需要将所有必需的参数传递给Portfolio.create!

Portfolio.create!(title: "Web App", subtitle: "asadasd", body: "sadsadas", main_image: 'some value', thumb_image: 'some image',
  technologies_attributes: [{name: "Ruby"}, {name: "Rails"}, {name: "Angula"}, {name: "Ionic"}])

更新:

在使用Rails 5.x时,在Rails 5.x(及更高版本)中,默认情况下,所有belongs_to关联均会验证(belongs_to关联)存在,因此将optional: true添加到您的技术模型中。 Read more about this

class Technology < ApplicationRecord 
  belongs_to :portfolio, optional: true 
end

答案 1 :(得分:0)

使belongs_to :portfolio关联为可选项并不能解决问题。您通过传递optional: true所做的所有事情都是跳过验证,如果您实际上想通过Model关联来设置数据库模式,则此操作没有用。

此问题的根本原因是Active Record试图在将Technology对象提交到数据库之前创建一个Portfolio对象。 accepts_nested_attributes_for的特殊之处在于它通过父模型(Technology)创建关联模型的实例(在这种情况下为Portfolio)。为此,Active Record需要了解两个模型之间的关系,并且还必须具有外键,以将关联的Technology对象绑定到特定的Portfolio

如果先创建一个Portfolio对象,然后再创建一个Technology对象,则不会有问题:

portfolio = Portfolio.create!(title: "Web App", subtitle: "asadasd", body: "sadsadas")

Technology.create!(name: 'aoeifjeao', portfolio_id: portfolio.id)
=> #<Technology id: 10, name: "aoeifjeao", portfolio_id: 17, created_at: "2019-12-07 03:54:47", updated_at: "2019-12-07 03:54:47"> 

如果您检查由Technology.create代码生成的SQL查询,则此工作的原因很明显:

(0.4ms)  BEGIN
Portfolio Load (0.7ms)  SELECT  "portfolios".* FROM "portfolios" WHERE "portfolios"."id" = $1 LIMIT $2  [["id", 17], ["LIMIT", 1]]
SQL (1.6ms)  INSERT INTO "technologies" ("name", "portfolio_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "aoeifjeao"], ["portfolio_id", 17], ["created_at", "2019-12-07 03:54:47.668662"], ["updated_at", "2019-12-07 03:54:47.668662"]]
(41.0ms)  COMMIT

请注意,发生的第一件事是Active Record从数据库中搜索具有特定Portfolio的{​​{1}}。然后,它将具有相同id的新记录插入technologies表中。

这是使用Active Record创建具有关联的模型时发生的情况。它首先使用您指定的portfolio_id在数据库中查找父模型,然后成为子模型的外键。

返回代码,当您调用id时,您实际上要做的是创建父对象和子对象,并建立它们的关联,而所有记录都将保存到数据库之前。 / p>

实际上发生的是Portfolio.create被分解为两个步骤。首先调用Portfolio.create,它实例化(但不保存)Portfolio.new对象:

Portfolio

请注意,我们的嵌套属性已经用于通过portfolio = Portfolio.new(title: "Web App", subtitle: "asadasd", body: "sadsadas", technologies_attributes: [{name: "Ruby"}]) => #<Portfolio id: nil, title: "Web App", subtitle: "asadasd", body: "sadsadas", main_image: "http://placehold.it/600x400", thumb_image: "http://placehold.it/350x200", created_at: nil, updated_at: nil> 关联实例化Technology对象,但是外键Portfolioportofolio_id

nil

接下来,调用portfolio.technologies => #<ActiveRecord::Associations::CollectionProxy [#<Technology id: nil, name: "Ruby", portfolio_id: nil, created_at: nil, updated_at: nil>]> ,这将引发Portfolio.save验证错误。这是在告诉您ActiveRecord::RecordInvalid对象需要一个Technology关联,但由于Portfolio仍然是portfolio_id,所以没有{@ 1}}。

设置nilbelongs_to关系时,Active Record会尝试通过试探法自动猜测逆关联,通常是通过查看表名来进行。通常这样做很不错,但是有时您需要明确并声明两个模型之间的has_many关系。

inverse_of
# portfolio.rb

class Portfolio < ApplicationRecord
  has_many :technologies, inverse_of: :portfolio
end