gpt4 book ai didi

ruby-on-rails - "undefined method ` 扫描 ' for nil:NilClass"以获取与 :inverse_of 的多态 ActiveRecord 关联

转载 作者:行者123 更新时间:2023-11-29 13:41:00 25 4
gpt4 key购买 nike

我有一个“has_many”关系的问题,我从 ActiveRecord 中得到一个随机异常:

product = Product.create!(valid_attributes)
product.prices
# throws:
NoMethodError:
undefined method `scan' for nil:NilClass

这似乎与“inverse_of”有关,但我显然做了一些 ActiveRecord 没有预料到的事情,但懒得去犯一个好的错误。最好的猜测是它与我名为“column”的专栏有关(尽管它不在黑名单 AFAIK 上)。我正在使用 PostgreSQL。 编辑:尝试将列重命名为“column_name”和“parent_column”,但没有解决问题。会尝试一些其他的事情。

这里是相关的模型代码和模式:

class Price < ApplicationRecord
belongs_to :parent, polymorphic: true
end

class Product < ApplicationRecord
has_many :prices, as: :parent, inverse_of: :parent
end

class CreatePrices < ActiveRecord::Migration[5.2]
def change
create_table :prices do |t|
t.string :parent_type, null: false
t.bigint :parent_id, null: false
t.string :column, null: false
t.decimal :price, null: false, precision: 15, scale: 2
t.timestamp :effective_date, null: false
end

add_index :prices, [:parent_type, :parent_id, :column]
end
end

以及完整的堆栈跟踪:

NoMethodError:
undefined method `scan' for nil:NilClass
# /Users/william/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/inheritance.rb:185:in `compute_type'
# /Users/william/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/reflection.rb:422:in `compute_class'
# /Users/william/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/reflection.rb:379:in `klass'
# /Users/william/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/reflection.rb:234:in `inverse_of'
# /Users/william/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/reflection.rb:239:in `check_validity_of_inverse!'
# /Users/william/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/reflection.rb:474:in `check_validity!'
# /Users/william/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/associations/association.rb:26:in `initialize'
# /Users/william/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/associations.rb:237:in `new'
# /Users/william/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/associations.rb:237:in `association'
# /Users/william/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/associations/builder/association.rb:108:in `prices'

最佳答案

答案是我以一种我没有意识到的方式违反了 ActiveRecord 的期望,并且应该包括在原始问题中,但由于傲慢而没有。 我在测试中使用了匿名模型。例如:

module Priced
extend ActiveSupport::Concern

included do
has_many :prices, as: :parent, inverse_of: :parent
end
end

RSpec.describe Priced do
let(:model_class) {
Class.new(ActiveRecord::Base).tap do |klass|
klass.table_name = "products"
klass.send(:include, Priced)
end
end

it "defines a 'has_many :prices' association" do
model = model_class.create!
price = Price.create!(parent: model, price: 100)
expect(model.prices).to eq([price])
end
end

这让我可以将 ActiveRecord 模型与现有表一起使用,但将其与实际 Product 模型中的所有逻辑分离。

我以前从未遇到过此设置的任何问题,因此多年来我一直愉快地使用它来测试模块。所以我认错了;如果我把这段代码放在原来的问题中,我可能会在发布之前就知道答案了,但我回答我自己的问题是为了警告其他“聪明的”Ruby 编码人员。

关于ruby-on-rails - "undefined method ` 扫描 ' for nil:NilClass"以获取与 :inverse_of 的多态 ActiveRecord 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55423472/

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