【问题标题】:Rails active_record_extended - With undefined method `with' for #<Class:0x00007f981bbb7118>)Rails active_record_extended - 对于#<Class:0x00007f981bbb7118> 使用未定义的方法“with”)
【发布时间】:2026-02-08 23:30:01
【问题描述】:

我需要使用 gem "active_record_extended",所以我已经添加到 Gemfile 并运行 "bundle"。

gem 'active_record_extended'

现在如果我尝试

irb(main):002:0> User.with
Traceback (most recent call last):
        2: from (irb):1
        1: from (irb):2:in `rescue in irb_binding'
NoMethodError (undefined method `with' for #<Class:0x00007fc6f43df208>)

我应该在某处需要宝石吗?我试图从 application.rb 中要求,但我遇到了问题。

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"

如果我从 Rails 控制台尝试这个

irb(main):002:0> require "active_record_extended"
=> false

Gemfile.lock

active_record_extended (1.4.0)
      activerecord (>= 5.0, < 6.1)
      ar_outer_joins (~> 0.2)
      pg (< 2.0)
activerecord (6.0.3.4)
  activemodel (= 6.0.3.4)
  activesupport (= 6.0.3.4)
activerecord-import (1.0.7)
  activerecord (>= 3.2)

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    为了将来的参考,如果你不把它放在一个组中,由于某种原因,这个 gem 将无法工作。我做了

    group :development, :test, :staging, :production do
      gem 'active_record_extended'
    end
    

    而且它完美无瑕。

    【讨论】:

      【解决方案2】:

      我认为在bundle install 之后您还没有重新启动 rails 控制台,这就是它不允许您使用的原因。

      【讨论】:

        【解决方案3】:

        您是否尝试在 with 方法中添加一些信息。

        如文档中的User.with(highly_liked: ProfileL.where("likes &gt; 300"))

        【讨论】:

        • 是的,同样的错误。如果我在 Rails 控制台中运行“require”active_record_extended”,我会得到错误。不应该是真的吗?
        • 尝试删除gemfile.lock并重新安装gem,我试过了,它对我有用,在rails console我也得到false
        • 还要检查你的 postgresql 是否是最新版本
        • 完成。同样的错误。 Postgres 13(使用 Postgres.app)。添加了新的 Gem file.lock
        • 我已经删除 gem "activerecord-import" 现在我得到 NoMethodError (undefined method `with' for Profile (调用 'Profile.connection' 建立连接):Class) for Profile.with(test : Profile.all)