宝石“审计活动记录",“~"4.0 给出了未定义的方法“审计" [英] Gem "audited-activerecord", "~> 4.0 gives undefined method `audits'

查看:46
本文介绍了宝石“审计活动记录",“~"4.0 给出了未定义的方法“审计"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:轨道 4.1.7红宝石 2.1.4

I am using: Rail 4.1.7 Ruby 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,请通过拉取请求告知我们."

I am using Ruby 2.1.4 and there is a warning sign "Audited may work just fine with a Ruby version not listed above (Latested 2.1.2), but we can't guarantee that it will. If you'd like to maintain a Ruby that isn't listed, please let us know with a pull request."

有人遇到过这种情况吗?

Has anyone experienced this before?

推荐答案

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

The audits method is an instance method, i.e. it is for one opportunity, not the entire Opportunity class.

您的代码:

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.

这篇关于宝石“审计活动记录",“~"4.0 给出了未定义的方法“审计"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆