Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined method with for Model #50

Closed
leoplct opened this issue Dec 21, 2020 · 22 comments
Closed

Undefined method with for Model #50

leoplct opened this issue Dec 21, 2020 · 22 comments

Comments

@leoplct
Copy link

leoplct commented Dec 21, 2020

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!
@leoplct leoplct changed the title Undefined method .with Undefined method with for Model Dec 21, 2020
@GeorgeKaraszi
Copy link
Owner

Which Rail's version are you running?

@leoplct
Copy link
Author

leoplct commented Dec 21, 2020

rails (6.0.3.4)
ruby 2.7.1

@GeorgeKaraszi
Copy link
Owner

Are you using classic or Zeitwerk autoloader?

@leoplct
Copy link
Author

leoplct commented Dec 22, 2020

I am not sure about. It's not present in the Gemfile, but i found this in Gemfile.lock. I've tried to delete Gemfile.lock and re "bundle install" but it's still being added to Gemfile.lock.

activesupport (6.0.3.4)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
      zeitwerk (~> 2.2, >= 2.2.2)
zeitwerk (2.4.2)

I have searched for the word "Zeitwerk" across all files and it's present only in Gemfile.lock. No other references.

@GeorgeKaraszi
Copy link
Owner

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"

@leoplct
Copy link
Author

leoplct commented Dec 22, 2020

I have tried both options but I got the same error. I have tried to comment all inside "Application" class but it's the same.

I think it's something related to the .with method.

This works -> Item.where.any(field: 'text')
This works -> Item.where.contains(field: ['text'])
This not works -> Profile.with(test: Profile.all)
This not works -> User.define_window(:no_order_name).partition_by(:email)

NoMethodError (undefined method define_window' for #<Class:0x00007fb1c4a406a0>) NoMethodError (undefined method with' for #Class:0x00007febc1cc15a0)

@GeorgeKaraszi
Copy link
Owner

GeorgeKaraszi commented Dec 22, 2020

Try replacing the gem with gem "active_record_extended", git: "https://github.com/GeorgeKaraszi/ActiveRecordExtended.git"

I haven't done a new release quite yet. And the current Master branch includes a lot of Rails 6.0 fixes.

Edit:

I wanna redact my zeitwerk auto loader issue. I just spun up a new Rails 6 project and saw no issue in regard to it.

@leoplct
Copy link
Author

leoplct commented Dec 22, 2020

Tried. Same error.

@GeorgeKaraszi
Copy link
Owner

It's hard to say tbh... I just created a new Rails 6.0.x project. And all I did was add the gem to the gemfile and its working for me, out of the box.

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.4'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

gem "active_record_extended", git: "https://github.com/GeorgeKaraszi/ActiveRecordExtended.git"

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'listen', '~> 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
$: rails c
> User.with(test: User.all).to_sql #=> "WITH \"test\" AS (SELECT \"users\".* FROM \"users\") SELECT \"users\".* FROM \"users\"" 

@leoplct
Copy link
Author

leoplct commented Dec 22, 2020

ok, It seems the issue it's caused by
gem 'resque'
it works if I remove this gem.

@GeorgeKaraszi
Copy link
Owner

hmmm, I'm glad your problem is solved. But it brings up another question as to why that gem would affect this one 🤔.

@Samsinite
Copy link

I'm getting the same error as well, don't have resque installed (we use sidedkiq).

@Samsinite
Copy link

@GeorgeKaraszi could it be due to how active record extended is being mixed into active record? What if it used the ActiveSupport.on_load :active_record do ... end hook to extend the functionality similar to how kaminari does it (https://github.com/kaminari/kaminari/blob/cd8601cc42b67267c15a13174bc2fc9bd5de1032/kaminari-activerecord/lib/kaminari/activerecord.rb).

@Samsinite
Copy link

I also do not see it in the ActiveRecord::Base.ancestors chain, or anything inheriting from ActiveRecord::Base, so I'm pretty sure the issue is how it is being included into ActiveRecord::Base.

@leoplct
Copy link
Author

leoplct commented Feb 9, 2021

Any updates on this?
I have updated to 2.0 but still getting the same error

User.with(test: User.all)
NoMethodError (undefined method `with' for #<Class:0x00007fdf113a6ef0>)

@skyeagle
Copy link

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!

@leoplct
Copy link
Author

leoplct commented Jun 3, 2021

Great! This works for me too.

@pirj
Copy link

pirj commented Dec 7, 2021

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

🤔

@pirj
Copy link

pirj commented Jan 11, 2022

Bump

@GeorgeKaraszi
Copy link
Owner

Made some significant changes to the gem's loading ability. I would suggest pulling the latest branch down to test if the recent changes has solved some of these lingering problems. I suspect this is a chicken or egg problem with what is getting loaded first.

@skyeagle
Copy link

Made some significant changes to the gem's loading ability. I would suggest pulling the latest branch down to test if the recent changes has solved some of these lingering problems. I suspect this is a chicken or egg problem with what is getting loaded first.

I've tested the master branch, and it seems to be fixing the issue. Thank you for your excellent work on this gem!

@GeorgeKaraszi
Copy link
Owner

closing this issue since recent changes might have resolved this concern. Open a new issue if this persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants