与:inverse_of的多态ActiveRecord关联的“未定义方法'scan'for nil:NilClass”

时间:2019-03-29 18:21:20

标签: ruby-on-rails ruby postgresql activerecord

我有一个“ 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'

1 个答案:

答案 0 :(得分:0)

答案是,我以某种我没有意识到的方式违反了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编码人员。