dynamic_matchers.rb:55:in `method_missing': undefined method `migration_error=' for ActiveRecord::Base:Class (NoMethodError)

匿名 (未验证) 提交于 2019-12-03 01:07:01

问题:

I'm on Windows. Ruby v.1.9.3p392 / Rails v. 3.2.13 - This is the demo_app project from the 2nd chapter of Michael Hart's Ruby on Rails Tutorial.

This error comes up when I issue 'rails generate scaffold User name:string email:string' Any idea how to fix this?

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 '         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 '         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 `'         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 `'         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 `'         from bin/rails:4:in `require'         from bin/rails:4:in `
'

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 User.find_by_user_name(user_name) and     # 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 find_all_by_amount(amount, options).     #     # Each dynamic finder using scoped_by_* 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  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:

ok, so someone else was able to find the solution to this problem and i'll share it here in case it comes up for someone else.

Apparently, the app I was creating was generating an old rails version even though my rails versions was set to a recent one. 3.2.13

Therefore my config/application.rb file was completely different than a rails 3.2.13 version. After the application.rb file was replaced. The errors were fixed.

To avoid this error, you can either use rvm or pik if you're a windows user to control your current version. You can specify your rails version when creating a new app 'rails 3.2.13 your_app', or uninstall all rails versions and just install the one you need:

gem uninstall rails -choose all versions

gem install rails --version '= 3.2.13'



回答2:

Thanks for the advice (+1), I had the same problem...

Created the app initially with an older version of rails and bumped it to the lastest (3.2.13) which broke my devise stuff..

All that was needed was to add require "rails/all" to the top of config/application.rb



回答3:

I had a similar problem that was complaining about an undefined method: ' has_many'. I spent a while fiddling with ruby and rails versions and gems before realizing the problem... the extra spaces before has_many!

I had copied old code from another place (Evernote, which uses all sorts of weird formatting) and didn't realize the whitespace details could mess things up like that. Deleting the spaces and putting them back in manually fixed the problem.

(This isn't really an answer to the original question, but my searches led me here and might lead others here.)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!