升级到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
我发现在滑轨5中它有些变化->将ArgumentError
或nil
传递到false
时提高Relation#merge
。
这些不是要合并到关系中的有效值,因此应及早警告用户。
我做了以下非常丑陋的事情:
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
答案 0 :(得分:0)
当前已通过以下操作以通过测试,但仍在调查和寻找更好的解决方案:
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))
}