RoR extend ActiveRecord::relation with undestroy_all

activerecord, ruby, ruby-on-rails

Solution

When you call `ActiveRecord::Relation.extend Track::TrackRelation` you are mixing the `Track::TrackRelation` methods into `ActiveRecord::Relation` as class methods.

What you want to do is mix in those same methods as instance methods. You can do that using `include` rather than `extend`. However `Module#include` is private. So one way to achieve what you want is:

ActiveRecord::Relation.send(:include, Track::TrackRelation)

Problem

I basically want to the following function, but in the reverse, and i already have the `undestroy` function working for single classes. https://github.com/rails/rails/blob/2ad168ee41d590bd9a4d15eddf3c2f719c23b60a/activerecord/lib/active_record/relation.rb#L364 However, I am trying to to extend ActiveRecord::Relation to no avail. Here's how i do it for other methods by for `ActiveRecord::Base` `ActiveRecord::Base.extend Track::BaseTrack` But using `ActiveRecord::Relation.extend Track::TrackRelation` doesn't seem to do anything. The module `TrackRelation` (inside `Track`) is: ``` module TrackRelation def undestroy_all(conditions = nil) if conditions where(conditions).undestroy_all else to_a.each {|object| object.undestroy }.tap { reset } end end end ``` Am I using the correct ActiveRecord class for relations? The error is: `undefined method "undestroy_all" for #<ActiveRecord::Relation []>`

Original source