Patch ActiveRecord::Relation#references and clean up