4

我对 rails 4 应用程序有疑问。在我的用户模型中(实际上,它与任何其他模型都相同)我不能使用with_options方法。如果我有这样的代码:

class User < ActiveRecord::Base

  # ...

  with_options :unless => :is_person do |u|
    u.validates :company_name, presence: true
    u.validates :company_address, presence: true
  end

  # ...
end

在我的应用程序启动时,我会收到这样的消息:

> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/mike/.rvm/gems/ruby-2.0.0-p0@leo4/gems/activerecord-4.0.0/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `  with_options' for #<Class:0x007fed97c0a6e8> (NoMethodError)
    from /Users/mike/dev/leo4/app/models/user.rb:57:in `<class:User>'
...

图片是一样的,如果这个块是空的,根本没有验证。发生了一些非常奇怪的事情,因为如果我只是删除这些行,启动控制台并检查它,该方法就存在。看起来它只是启动时间的问题(?)

有什么想法吗?谢谢!

PS我的Gemfile.lock:

GIT
  remote: git://github.com/gregbell/active_admin.git
  revision: 72a9c30cef47b3f38c38039de021aaa3194c188b
  branch: rails4
  specs:
    activeadmin (0.6.0)
      arbre (>= 1.0.1)
      bourbon (>= 1.0.0)
      coffee-rails (>= 3.2.0)
      devise (>= 3.0.2)
      formtastic (>= 2.3.0.rc2)
      inherited_resources (>= 1.4.1)
      jquery-rails (>= 3.0.0)
      jquery-ui-rails (>= 4.0.0)
      kaminari (>= 0.13.0)
      rails (>= 3.2.0)
      ransack (>= 1.0.0)
      sass-rails (>= 3.2.0)

GIT
  remote: git://github.com/justinfrench/formtastic.git
  revision: cd6cb88f28ea7444267802ced8e5ec0f5cfd0f90
  branch: master
  specs:
    formtastic (2.3.0.rc2)
      actionpack (>= 3.0)

GIT
  remote: git://github.com/sporkrb/spork-rails.git
  revision: 3224f84d8c31fcb0894e9a43f6c3ac67e3aa0d71
  specs:
    spork-rails (3.2.1)
      rails (>= 3.0.0, < 5)
      spork (>= 1.0rc0)

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.0.0)
      actionpack (= 4.0.0)
      mail (~> 2.5.3)
    actionpack (4.0.0)
      activesupport (= 4.0.0)
      builder (~> 3.1.0)
      erubis (~> 2.7.0)
      rack (~> 1.5.2)
      rack-test (~> 0.6.2)
    activemodel (4.0.0)
      activesupport (= 4.0.0)
      builder (~> 3.1.0)
    activerecord (4.0.0)
      activemodel (= 4.0.0)
      activerecord-deprecated_finders (~> 1.0.2)
      activesupport (= 4.0.0)
      arel (~> 4.0.0)
    activerecord-deprecated_finders (1.0.3)
    activesupport (4.0.0)
      i18n (~> 0.6, >= 0.6.4)
      minitest (~> 4.2)
      multi_json (~> 1.3)
      thread_safe (~> 0.1)
      tzinfo (~> 0.3.37)
    annotate (2.5.0)
      rake
    arbre (1.0.1)
      activesupport (>= 3.0.0)
    arel (4.0.0)
    atomic (1.1.13)
    bcrypt-ruby (3.1.1)
    bourbon (3.1.8)
      sass (>= 3.2.0)
      thor
    builder (3.1.4)
    cancan (1.6.10)
    capybara (2.1.0)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      xpath (~> 2.0)
    capybara-webkit (1.0.0)
      capybara (~> 2.0, >= 2.0.2)
      json
    coffee-rails (4.0.0)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0.beta, < 5.0)
    coffee-script (2.2.0)
      coffee-script-source
      execjs
    coffee-script-source (1.6.3)
    cucumber (1.3.7)
      builder (>= 2.1.2)
      diff-lcs (>= 1.1.3)
      gherkin (~> 2.12.1)
      multi_json (~> 1.7.5)
      multi_test (>= 0.0.2)
    database_cleaner (1.0.1)
    devise (3.0.2)
      bcrypt-ruby (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 3.2.6, < 5)
      warden (~> 1.2.3)
    diff-lcs (1.2.4)
    draper (1.2.1)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      request_store (~> 1.0.3)
    erubis (2.7.0)
    execjs (1.4.0)
      multi_json (~> 1.0)
    factory_girl (4.2.0)
      activesupport (>= 3.0.0)
    factory_girl_rails (4.2.1)
      factory_girl (~> 4.2.0)
      railties (>= 3.0.0)
    faraday (0.8.8)
      multipart-post (~> 1.2.0)
    ffaker (1.18.0)
    gherkin (2.12.1)
      multi_json (~> 1.3)
    has_scope (0.6.0.rc)
      actionpack (>= 3.2, < 5)
      activesupport (>= 3.2, < 5)
    hashie (2.0.5)
    hike (1.2.3)
    httpauth (0.2.0)
    i18n (0.6.5)
    inherited_resources (1.4.1)
      has_scope (~> 0.6.0.rc)
      responders (~> 1.0.0.rc)
    jbuilder (1.4.2)
      activesupport (>= 3.0.0)
      multi_json (>= 1.2.0)
    jquery-rails (3.0.4)
      railties (>= 3.0, < 5.0)
      thor (>= 0.14, < 2.0)
    jquery-ui-rails (4.0.4)
      jquery-rails
      railties (>= 3.1.0)
    json (1.8.0)
    jwt (0.1.8)
      multi_json (>= 1.5)
    kaminari (0.14.1)
      actionpack (>= 3.0.0)
      activesupport (>= 3.0.0)
    libv8 (3.11.8.17)
    mail (2.5.4)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    mime-types (1.23)
    mini_portile (0.5.1)
    minitest (4.7.5)
    multi_json (1.7.9)
    multi_test (0.0.2)
    multipart-post (1.2.0)
    nokogiri (1.6.0)
      mini_portile (~> 0.5.0)
    oauth2 (0.8.1)
      faraday (~> 0.8)
      httpauth (~> 0.1)
      jwt (~> 0.1.4)
      multi_json (~> 1.0)
      rack (~> 1.2)
    omniauth (1.1.4)
      hashie (>= 1.2, < 3)
      rack
    omniauth-facebook (1.4.1)
      omniauth-oauth2 (~> 1.1.0)
    omniauth-mailru (1.0.0)
      omniauth (~> 1.0)
      omniauth-oauth2 (~> 1.0)
    omniauth-oauth2 (1.1.1)
      oauth2 (~> 0.8.0)
      omniauth (~> 1.0)
    omniauth-odnoklassniki (0.0.4)
      omniauth (~> 1.0)
      omniauth-oauth2 (~> 1.0)
    omniauth-vkontakte (1.2.0)
      multi_json
      omniauth (~> 1.1.0)
      omniauth-oauth2 (~> 1.0)
    orm_adapter (0.4.0)
    polyamorous (0.6.3)
      activerecord (>= 3.0)
    polyglot (0.3.3)
    rack (1.5.2)
    rack-test (0.6.2)
      rack (>= 1.0)
    rails (4.0.0)
      actionmailer (= 4.0.0)
      actionpack (= 4.0.0)
      activerecord (= 4.0.0)
      activesupport (= 4.0.0)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.0.0)
      sprockets-rails (~> 2.0.0)
    railties (4.0.0)
      actionpack (= 4.0.0)
      activesupport (= 4.0.0)
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    rake (10.1.0)
    ransack (1.0.0)
      actionpack (>= 3.0)
      activerecord (>= 3.0)
      polyamorous (~> 0.6.0)
    rdiscount (2.1.6)
    rdoc (3.12.2)
      json (~> 1.4)
    redis (3.0.4)
    redis-actionpack (4.0.0)
      actionpack (~> 4)
      redis-rack (~> 1.5.0)
      redis-store (~> 1.1.0)
    redis-activesupport (4.0.0)
      activesupport (~> 4)
      redis-store (~> 1.1.0)
    redis-rack (1.5.0)
      rack (~> 1.5)
      redis-store (~> 1.1.0)
    redis-rails (4.0.0)
      redis-actionpack (~> 4)
      redis-activesupport (~> 4)
      redis-store (~> 1.1.0)
    redis-store (1.1.4)
      redis (>= 2.2)
    ref (1.0.5)
    request_store (1.0.5)
    responders (1.0.0.rc)
      railties (>= 3.2, < 5)
    rspec-core (2.14.5)
    rspec-expectations (2.14.2)
      diff-lcs (>= 1.1.3, < 2.0)
    rspec-mocks (2.14.3)
    rspec-rails (2.14.0)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      railties (>= 3.0)
      rspec-core (~> 2.14.0)
      rspec-expectations (~> 2.14.0)
      rspec-mocks (~> 2.14.0)
    sass (3.2.10)
    sass-rails (4.0.0)
      railties (>= 4.0.0.beta, < 5.0)
      sass (>= 3.1.10)
      sprockets-rails (~> 2.0.0)
    sdoc (0.3.20)
      json (>= 1.1.3)
      rdoc (~> 3.10)
    simple_form (1.4.1)
    simplecov (0.7.1)
      multi_json (~> 1.0)
      simplecov-html (~> 0.7.1)
    simplecov-html (0.7.1)
    slim (2.0.1)
      temple (~> 0.6.6)
      tilt (>= 1.3.3, < 2.1)
    slim-rails (2.0.1)
      actionpack (>= 3.0, < 4.1)
      activesupport (>= 3.0, < 4.1)
      railties (>= 3.0, < 4.1)
      slim (~> 2.0)
    spork (1.0.0rc3)
    sprockets (2.10.0)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    sprockets-rails (2.0.0)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      sprockets (~> 2.8)
    sqlite3 (1.3.7)
    temple (0.6.6)
    therubyracer (0.11.4)
      libv8 (~> 3.11.8.12)
      ref
    thor (0.18.1)
    thread_safe (0.1.2)
      atomic
    tilt (1.4.1)
    treetop (1.4.14)
      polyglot
      polyglot (>= 0.3.1)
    turbolinks (1.3.0)
      coffee-rails
    tzinfo (0.3.37)
    uglifier (2.1.2)
      execjs (>= 0.3.0)
      multi_json (~> 1.0, >= 1.0.2)
    warden (1.2.3)
      rack (>= 1.0)
    xpath (2.0.0)
      nokogiri (~> 1.3)

PLATFORMS
  ruby

DEPENDENCIES
  activeadmin!
  annotate (>= 2.5.0)
  cancan (= 1.6.10)
  capybara
  capybara-webkit
  coffee-rails (~> 4.0.0)
  cucumber
  database_cleaner (< 1.1.0)
  devise (= 3.0.2)
  draper (~> 1.0)
  factory_girl_rails
  ffaker
  formtastic!
  jbuilder (~> 1.2)
  jquery-rails (= 3.0.4)
  kaminari (= 0.14.1)
  omniauth (= 1.1.4)
  omniauth-facebook
  omniauth-mailru
  omniauth-odnoklassniki
  omniauth-vkontakte
  rails (= 4.0.0)
  rdiscount
  redis-rails (~> 4.0.0)
  rspec-rails (~> 2.0)
  sass-rails (~> 4.0.0)
  sdoc
  simple_form (= 1.4.1)
  simplecov
  slim-rails (= 2.0.1)
  spork-rails!
  sqlite3
  therubyracer
  turbolinks (= 1.3.0)
  uglifier (>= 1.3.0)
4

1 回答 1

0

需要 ActiveSupport:

require 'active_support/all'

于 2017-05-02T13:37:04.693 回答