ActiveRecord::Relation.concat 在 Rails 5 中失败

ActiveRecord::Relation.concat failing in Rails 5

愚蠢的问题,但我不确定为什么这在 Rails 4.2 中有效,但在 Rails 5.2 中无效。

FamilyCharacteristic.where(family_id: @user.family_ids)
                    .concat(@user.characteristics)

规范在 5.2 中失败:

Failure/Error:
       FamilyCharacteristic.where(family_id: @user.family_ids)
                           .concat(@user.characteristics)

     NoMethodError:
       undefined method `concat' for #<ActiveRecord::Relation []>
       Did you mean?  count

concat 是从 5.2 中的 ActiveRecord::Relation 中删除的,还是 FamilyCharacteristic.where(family_id: @user.family_ids) 以某种方式在 < 4.2 中是不同的对象?

感谢您的帮助。

我做了一些挖掘,发现:

  • FamilyCharacteristic.where(family_id: @user.family_ids)的class没变,还是ActiveRecord::Relation
  • Relation 没有而且仍然没有定义它自己的 concat 方法,但是它被委托给 Array#concat 直到 this commit happened,所以在 Rails 4.2 SomeModel.where(id: ids).concat(some_records)(其中 returns 和 Array)实际上与 SomeModel.where(id: ids).to_a.concat(some_models)
  • 相同
  • ActiveRecord::Delegation 中的 mentioned before change 之后,在 Rails 5.2 中,唯一委托给 Array 的方法是本模块中指定的方法,而 concat 是不在其中

总而言之 - 您示例中的 concat 从来都不是 ActiveRecord 的一部分,而是委托给 Array#concat,这就是它起作用的原因。它不再在 Rails 5 中委派,因此它抛出 NoMethodError.