gpt4 book ai didi

ruby-on-rails - ActiveRecord::RecordInvalid:验证失败:技术组合必须存在

转载 作者:行者123 更新时间:2023-12-02 14:54:36 25 4
gpt4 key购买 nike

我试图用组合保存技术的嵌套属性,我有以下代码:投资组合.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')

最佳答案

使 belongs_to :portfolio 关联可选并不能解决问题。您通过传入 optional: true 所做的一切都是在跳过验证,如果您真的想设置一个带有模型关联的数据库模式,这就没有用了。

问题的根本原因是 Active Record 试图在 Portfolio 对象提交到数据库之前创建一个 Technology 对象。 accepts_nested_attributes_for 的特殊之处在于它通过父模型(Portfolio)创建关联模型(在本例中为Technology)的实例。为此,Active Record 需要知道两个模型之间的关系,并且它还必须有一个外键来将关联的 Technology 对象绑定(bind)到特定的 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 从数据库中查找具有特定 idPortfolio。然后它使用相同的 portfolio_id 将新记录插入到 technologies 表中。

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

回到您的代码,当您调用 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>

请注意,我们的嵌套属性已经用于通过Portfolio 关联实例化一个Technology 对象,但是外键portofolio_id:

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 仍然是 nil.

当设置 belongs_tohas_many 关系时,Active Record 会尝试基于启发式算法自动猜测反向关联,通常是通过查看表名。它通常很擅长,但有时您需要明确并声明两个模型之间的 inverse_of 关系。

# portfolio.rb

class Portfolio < ApplicationRecord
has_many :technologies, inverse_of: :portfolio
end
# technology.rb

class Technology < ApplicationRecord
belongs_to :portfolio, inverse_of: :technologies
end

关于ruby-on-rails - ActiveRecord::RecordInvalid:验证失败:技术组合必须存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53586779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com