愚蠢的问题,但我不确定为什么这在 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)
的类没变,还是ActiveRecord::Relation
Relation
没有并且仍然没有定义它自己的concat
方法,但它被委托(delegate)给Array#concat
直到 this commit happened ,所以在 Rails 4.2SomeModel.where(id: ids).concat(some_records)
(它返回一个Array
)实际上和SomeModel.where(id : ids).to_a.concat(some_models)
- 在
ActiveRecord::Delegation
中的 mentioned before change 之后,在 Rails 5.2 中,唯一委托(delegate)给Array
的方法是此模块中指定的方法和concat
不在其中
总而言之 - 您示例中的 concat
从来都不是 ActiveRecord
的一部分,而是委托(delegate)给了 Array#concat
,这就是它起作用的原因。它不再在 Rails 5 中委托(delegate),因此它会抛出 NoMethodError
。
关于ruby-on-rails - ActiveRecord::Relation.concat 在 Rails 5 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53125581/