ActiveRecordExtended: Undefined method with for Model

I have added gem 'active_record_extended' then bundle

but I get this error

irb(main):005:0> Profile.with(test: Profile.all)
Traceback (most recent call last):
        1: from (irb):5
NoMethodError (undefined method `with' for #<Class:0x00007fab63c67c88>)
irb(main):007:0> Profile.public_methods.include?(:with)
=> false

What could be the reason?

Gemfile.lock

active_record_extended (1.4.0)
      activerecord (>= 5.0, < 6.1)
      ar_outer_joins (~> 0.2)
      pg (< 2.0)

config/boot.rb

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.

config/application.rb

require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
# CUSTOM
require "active_record_extended"

config/enviroment.rb

# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 22 (8 by maintainers)

Most upvoted comments

I’ve found a workaround for now:

config/application.rb

require "rails/all"

require "active_record_extended"  # <-- add this before rails configuration 

thanks for the amazing gem!

The gem includes itself:

ActiveRecord::Relation.prepend(ActiveRecordExtended::QueryMethods::Unionize)

To my best knowledge, this is prone to load order errors. To be a good citizen, Railtie mechanism would better be used, e.g:

ActiveSupport.on_load :active_record do
  ActiveRecord::Relation.prepend ActiveRecordExtended::QueryMethods::Unionize
end

See example and doc.

Another lead that might reveal the mystery is one of the comments above:

Item.where.any(field: 'text') # works
Profile.with(test: Profile.all) # doesn't work

Is it ActiveRecord::Relation that is the right one to extend?

class C < ActiveRecord::Base
end
C.ancestors.include?(ActiveRecord::Relation)
=> false

🤔

If you never went into your application.rb file and explicitly set the config to use classic loader, then I got a feeling its the cause of not only your’s, but other peoples issues too.

couple of temp fixes you can try until I can address zeitwerk:

in your config/application.rb file, insert this line: config.autoloader = :classic

otherwise you can add require "active_record_extended" after the require "rails"