Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord::Relation.concat failing in Rails 5

Stupid question but I'm not sure why this would work in Rails 4.2 but not in Rails 5.2.

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

Specs fail in 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

Was concat removed from ActiveRecord::Relation in 5.2 or was FamilyCharacteristic.where(family_id: @user.family_ids) somehow a different object in < 4.2?

Thanks for any help.

like image 439
Meltemi Avatar asked Nov 02 '18 20:11

Meltemi


1 Answers

I did some digging and found out that:

  • FamilyCharacteristic.where(family_id: @user.family_ids)'s class didn't change, it's still ActiveRecord::Relation
  • Relation didn't and still doesn't define its own concat method, but it was delegated to Array#concat until this commit happened, so in Rails 4.2 SomeModel.where(id: ids).concat(some_records)(which returns an Array) was actually the same as SomeModel.where(id: ids).to_a.concat(some_models)
  • After mentioned before change in ActiveRecord::Delegation, in Rails 5.2, the only methods delegated to Array are the ones specified in this module and concat is not among them

To sum up - concat from your example was never part of ActiveRecord but was delegated to Array#concat and that's why it worked. It's no longer delegated in Rails 5 so it throws NoMethodError.

like image 191
ahawrylak Avatar answered Oct 05 '22 11:10

ahawrylak