升级到rails 5.1.7后出现错误ArgumentError:无效参数:nil升级到rails 5.1.7后,



出现错误ArgumentError:无效参数:nil。

测试:

describe 'default_for scope' do
it 'joins intel_tags' do
scope = IntelTagging.default_for('something')
expect(scope.joins_values).to include(:intel_tag)
end
it 'uses IntelTag.default_for' do
expect(IntelTag).to receive(:default_for).with('something')
IntelTagging.default_for('something')
end
end

型号:

class IntelTagging < ApplicationRecord
...
belongs_to :intel_tag, inverse_of: :intel_taggings, optional: true
accepts_nested_attributes_for :intel_tag
validates_presence_of :intel_tag
scope :default_for, ->(type) {
joins(:intel_tag).merge(IntelTag.default_for(type))
}
scope :key, ->(key) { joins(:intel_tag).merge(IntelTag.key(key)) }
...
end
class IntelTag < ApplicationRecord
...
has_many :intel_taggings, inverse_of: :intel_tag, dependent: :destroy
scope :default_for, ->(string) { where(arel_table[:default_for].matches("%#{string}%")) }
...
end

我发现在rails 5中,当将nilfalse传递到Relation#merge时,它有点变化->升高ArgumentError。这些值不是在关系中合并的有效值,因此它应该尽早警告用户。

我做了以下相当丑陋的事情:

scope :default_for, ->(type) {
joins_intel_tag = joins(:intel_tag)
joins_intel_tag.merge(IntelTag.default_for(type)) if joins_intel_tag.present?
}

但仍有错误

(IntelTag(id: integer, managed: boolean, default_for: text, unmanaged_name: string, name_en: string, name_fr: string, name_it: string, name_de: string, deleted_at: datetime, created_at: datetime, updated_at: datetime, key: string) (class)).default_for("something")
expected: 1 time with arguments: ("something")
received: 0 times

目前做了以下工作以使测试通过,但仍在调查&寻找更好的解决方案:

scope :default_for, ->(type) {
joins(:intel_tag).merge(IntelTag.default_for(type)) unless IntelTag.default_for(type).nil?
}
scope :key, ->(key) {
IntelTag.key(key).nil? ? joins(:intel_tag) : joins(:intel_tag).merge(IntelTag.key(key))
}

最新更新