Gem "audited-activerecord", "~> 4.0 给出了未定义的方法 `audits'

Posted

技术标签:

【中文标题】Gem "audited-activerecord", "~> 4.0 给出了未定义的方法 `audits\'【英文标题】:Gem "audited-activerecord", "~> 4.0 gives undefined method `audits'Gem "audited-activerecord", "~> 4.0 给出了未定义的方法 `audits' 【发布时间】:2015-02-05 19:33:15 【问题描述】:

我正在使用: 铁路 4.1.7 红宝石 2.1.4

我刚刚安装了Audited GEM:

gem "audited-activerecord", "~> 4.0"
$ rails generate audited:upgrade
$ rake db:migrate

型号

class Opportunity < ActiveRecord::Base
    ...
    audited
    ...
end

当我跑步时:

Opportunity.audits.count

我收到此错误:

2.1.4 :001 > Opportunity.audits.count
NoMethodError: undefined method `audits' for #<Class:0x007ff7946656f0>
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/activerecord-4.1.7/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
    from (irb):1
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/railties-4.1.7/lib/rails/commands/console.rb:90:in `start'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/railties-4.1.7/lib/rails/commands/console.rb:9:in `start'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/railties-4.1.7/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/railties-4.1.7/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/railties-4.1.7/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `require'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `require'
    from /Users/andreucasadella/rails_projects/crm/bin/rails:8:in `<top (required)>'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `block in load'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/spring-1.2.0/lib/spring/commands/rails.rb:6:in `call'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/spring-1.2.0/lib/spring/command_wrapper.rb:38:in `call'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/spring-1.2.0/lib/spring/application.rb:183:in `block in serve'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/spring-1.2.0/lib/spring/application.rb:156:in `fork'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/spring-1.2.0/lib/spring/application.rb:156:in `serve'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/spring-1.2.0/lib/spring/application.rb:131:in `block in run'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/spring-1.2.0/lib/spring/application.rb:125:in `loop'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/spring-1.2.0/lib/spring/application.rb:125:in `run'
    from /Users/andreucasadella/.rvm/gems/ruby-2.1.4/gems/spring-1.2.0/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /Users/andreucasadella/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/andreucasadella/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'2.1.4

我正在使用 Ruby 2.1.4 并且有一个警告标志“经过审核的可能适用于上面未列出的 Ruby 版本(最新的 2.1.2),但我们不能保证它会。如果你愿意想维护未列出的 Ruby,请通过拉取请求告诉我们。”

以前有人经历过吗?

【问题讨论】:

【参考方案1】:

audits 方法是一个实例方法,即它只针对一个机会,而不是整个机会类。

您的代码:

Opportunity.audits.count  # Fails because audits is not a class method.

解决方案代码:

opportunity = Opportunity.first  # any search you want
opportunity.audits.count  # Succeeds because audits is an instance method.

【讨论】:

以上是关于Gem "audited-activerecord", "~> 4.0 给出了未定义的方法 `audits'的主要内容,如果未能解决你的问题,请参考以下文章

标准+宝石+博客

Gem "audited-activerecord", "~> 4.0 给出了未定义的方法 `audits'

Bundler 找不到 gem "rails" 的兼容版本:用于狂欢

Bundler 找不到 gem "spring" 的兼容版本

Rails Gem "Axlsx" - 重命名工作簿

Gitlab 安装“gem”失败