dynamic_matchers.rb:55:in `method_missing':ActiveRecord::Base:Class 的未定义方法 `migration_error=' (NoMet

Posted

技术标签:

【中文标题】dynamic_matchers.rb:55:in `method_missing\':ActiveRecord::Base:Class 的未定义方法 `migration_error=\' (NoMethodError)【英文标题】:dynamic_matchers.rb:55:in `method_missing': undefined method `migration_error=' for ActiveRecord::Base:Class (NoMethodError)dynamic_matchers.rb:55:in `method_missing':ActiveRecord::Base:Class 的未定义方法 `migration_error=' (NoMethodError) 【发布时间】:2013-04-28 04:16:18 【问题描述】:

我在 Windows 上。 Ruby v.1.9.3p392 / Rails v. 3.2.13 - 这是 Michael Hart 的 Ruby on Rails 教程第 2 章中的 demo_app 项目。

当我发出“rails generate scaffold User name:string email:string”时出现此错误,知道如何解决这个问题吗?

C:\ruby\rails_projects\demo_app>rails generate scaffold User name:string email:s
tring
      invoke  active_record
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/act
ive_record/dynamic_matchers.rb:55:in `method_missing': undefined method `migrati
on_error=' for ActiveRecord::Base:Class (NoMethodError)
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3
.2.13/lib/active_record/railtie.rb:66:in `block (3 levels) in <class:Railtie>'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3
.2.13/lib/active_record/railtie.rb:65:in `each'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3
.2.13/lib/active_record/railtie.rb:65:in `block (2 levels) in <class:Railtie>'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.13/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.13/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.13/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.13/lib/active_support/lazy_load_hooks.rb:42:in `each'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.13/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3
.2.13/lib/active_record/base.rb:720:in `<top (required)>'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1
3/lib/rails/generators/named_base.rb:166:in `pluralize_table_names?'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1
3/lib/rails/generators/named_base.rb:114:in `table_name'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3
.2.13/lib/rails/generators/active_record/model/model_generator.rb:17:in `create_
migration_file'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/command.rb:27:in `run'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/invocation.rb:120:in `invoke_command'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/invocation.rb:127:in `block in invoke_all'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/invocation.rb:127:in `each'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/invocation.rb:127:in `map'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/invocation.rb:127:in `invoke_all'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/group.rb:233:in `dispatch'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/invocation.rb:109:in `invoke'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/group.rb:278:in `block in _invoke_for_class_method'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/shell.rb:74:in `with_padding'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/group.rb:267:in `_invoke_for_class_method'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/group.rb:150:in `_invoke_from_option_orm'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/command.rb:27:in `run'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/invocation.rb:120:in `invoke_command'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/invocation.rb:127:in `block in invoke_all'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/invocation.rb:127:in `each'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/invocation.rb:127:in `map'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/invocation.rb:127:in `invoke_all'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/group.rb:233:in `dispatch'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/thor-0.18.1/li
b/thor/base.rb:439:in `start'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1
3/lib/rails/generators.rb:171:in `invoke'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1
3/lib/rails/commands/generate.rb:12:in `<top (required)>'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.13/lib/active_support/dependencies.rb:251:in `require'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.13/lib/active_support/dependencies.rb:251:in `block in require'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
3.2.13/lib/active_support/dependencies.rb:251:in `require'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1
3/lib/rails/commands.rb:29:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

dynamic_matchers.rb

module ActiveRecord
  module DynamicMatchers
    def respond_to?(method_id, include_private = false)
      if match = DynamicFinderMatch.match(method_id)
        return true if all_attributes_exists?(match.attribute_names)
      elsif match = DynamicScopeMatch.match(method_id)
        return true if all_attributes_exists?(match.attribute_names)
      end

      super
    end

    private

    # Enables dynamic finders like <tt>User.find_by_user_name(user_name)</tt> and
    # <tt>User.scoped_by_user_name(user_name). Refer to Dynamic attribute-based finders
    # section at the top of this file for more detailed information.
    #
    # It's even possible to use all the additional parameters to +find+. For example, the
    # full interface for +find_all_by_amount+ is actually <tt>find_all_by_amount(amount, options)</tt>.
    #
    # Each dynamic finder using <tt>scoped_by_*</tt> is also defined in the class after it
    # is first invoked, so that future attempts to use it do not run through method_missing.
    def method_missing(method_id, *arguments, &block)
      if match = (DynamicFinderMatch.match(method_id) || DynamicScopeMatch.match(method_id))
        attribute_names = match.attribute_names
        super unless all_attributes_exists?(attribute_names)
        if !(match.is_a?(DynamicFinderMatch) && match.instantiator? && arguments.first.is_a?(Hash)) && arguments.size < attribute_names.size
          method_trace = "#__FILE__:#__LINE__:in `#method_id'"
          backtrace = [method_trace] + caller
          raise ArgumentError, "wrong number of arguments (#arguments.size for #attribute_names.size)", backtrace
        end
        if match.respond_to?(:scope?) && match.scope?
          self.class_eval <<-METHOD, __FILE__, __LINE__ + 1
            def self.#method_id(*args)                                    # def self.scoped_by_user_name_and_password(*args)
              attributes = Hash[[:#attribute_names.join(',:')].zip(args)] #   attributes = Hash[[:user_name, :password].zip(args)]
                                                                            #
              scoped(:conditions => attributes)                             #   scoped(:conditions => attributes)
            end                                                             # end
          METHOD
          send(method_id, *arguments)
        elsif match.finder?
          options = if arguments.length > attribute_names.size
                      arguments.extract_options!
                    else
                      
                    end

          relation = options.any? ? scoped(options) : scoped
          relation.send :find_by_attributes, match, attribute_names, *arguments, &block
        elsif match.instantiator?
          scoped.send :find_or_instantiator_by_attributes, match, attribute_names, *arguments, &block
        end
      else
        super
      end
    end

    # Similar in purpose to +expand_hash_conditions_for_aggregates+.
    def expand_attribute_names_for_aggregates(attribute_names)
      attribute_names.map  |attribute_name|
        unless (aggregation = reflect_on_aggregation(attribute_name.to_sym)).nil?
          aggregate_mapping(aggregation).map do |field_attr, _|
            field_attr.to_sym
          end
        else
          attribute_name.to_sym
        end
      .flatten
    end

    def all_attributes_exists?(attribute_names)
      (expand_attribute_names_for_aggregates(attribute_names) -
       column_methods_hash.keys).empty?
    end

    def aggregate_mapping(reflection)
      mapping = reflection.options[:mapping] || [reflection.name, reflection.name]
      mapping.first.is_a?(Array) ? mapping : [mapping]
    end


  end
end

【问题讨论】:

【参考方案1】:

好的,所以其他人能够找到解决这个问题的方法,我会在这里分享它以防其他人出现。

显然,我正在创建的应用程序正在生成旧的 rails 版本,即使我的 rails 版本设置为最近的版本。 3.2.13

因此,我的 config/application.rb 文件与 rails 3.2.13 版本完全不同。 application.rb 文件被替换后。错误已修复。

为避免此错误,如果您是 Windows 用户,您可以使用 rvm 或 pik 来控制当前版本。您可以在创建新应用“rails 3.2.13 your_app”时指定您的 rails 版本,或者卸载所有 rails 版本并安装您需要的版本:

gem 卸载 rails -选择所有版本

gem install rails --version '= 3.2.13'

【讨论】:

其实应该是'rails new demo_app 3.2.13'来指定一个应用的rails版本,如果需要的话。 对不起,这是正确的命令'rails 3.2.13 new newappname' 嗯,是的,版本规范仍然是错误的。大声笑,但它不需要。 rails 会默认安装最新的 ;)【参考方案2】:

感谢您的建议 (+1),我遇到了同样的问题...

最初使用旧版本的 rails 创建了应用程序,并将其撞到了最新版本 (3.2.13),这破坏了我的设计内容..

只需将require "rails/all" 添加到config/application.rb 的顶部

【讨论】:

【参考方案3】:

我有一个类似的问题是抱怨未定义的方法:'has_many'。在意识到问题之前,我花了一段时间摆弄 ruby​​ 和 rails 版本和 gems……has_many 之前的额外空格!

我从另一个地方复制了旧代码(Evernote,它使用了各种奇怪的格式),但没有意识到空格细节可能会把事情搞砸。删除空格并将它们手动放回修复了问题。

(这并不是对原始问题的真正答案,但我的搜索将我引到这里,也可能将其他人引到这里。)

【讨论】:

以上是关于dynamic_matchers.rb:55:in `method_missing':ActiveRecord::Base:Class 的未定义方法 `migration_error=' (NoMet的主要内容,如果未能解决你的问题,请参考以下文章