5

我陷入了一个非常奇怪的问题。我最近从 rails 4.1 更新到 4.2。在 4.1 中一切正常。但是现在,在 4.2 中,模型关注内容似乎被忽略了,即使找到了关注点。

说得简单。我有一个Client模型,它属于在关注点Address中定义的Addressable关系:

应用程序/模型/client.rb

class Client < ActiveRecord::Base
  include Addressable
end

应用程序/模型/地址.rb

class Address < ActiveRecord::Base
end

应用程序/模型/关注/addressable.rb

module Addressable
  extend ActiveSupport::Concern

  included do
    belongs_to :address
  end

环境没有问题development。我可以Client.new.address毫无问题地做。但是在test环境中,同一行给了我一个unknow method address错误。如果我搬到belongs_to :address那里Client,它会再次起作用。该模块被包含在内,因为这没有错误,如果我将名称更改为Adressabledewdewdedthen,当然会出现错误。

验证方法等也会发生同样的情况。

我一直在调试这个,但我没有找到根。我已经复制了我development.rb的 astest.rb并重新启动了服务器,但仍然如此。我无法在全新的 Rails 4.2 应用程序中重现这一点,因此它与我的应用程序或更新过程有关,但不知道在哪里。

有人有线索吗?

谢谢

更新

Client.new.address这是在测试环境中的控制台中执行的堆栈跟踪(在开发中它有效):

NoMethodError: undefined method `address' for #<Client:0xbc89dac0>
    from /home/marc/.gem/ruby/2.1.3/gems/activemodel-4.2.0/lib/active_model/attribute_methods.rb:433:in `method_missing'
    from (irb):3
    from /home/marc/.gem/ruby/2.1.3/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
    from /home/marc/.gem/ruby/2.1.3/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
    from /home/marc/.gem/ruby/2.1.3/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/marc/.gem/ruby/2.1.3/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/marc/.gem/ruby/2.1.3/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
    from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /home/marc/projects/clients_webs/a_project/bin/rails:8:in `<top (required)>'
    from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
    from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
    from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
    from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/commands/rails.rb:6:in `call'
    from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/command_wrapper.rb:38:in `call'
    from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:183:in `block in serve'
    from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:156:in `fork'
    from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:156:in `serve'
    from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:131:in `block in run'
    from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:125:in `loop'
    from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:125:in `run'
    from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /home/marc/.rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /home/marc/.rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'

更新

两种环境中的结果Client.ancestors是(当然,没有实例身份的唯一区别):

 => [Client (call 'Client.connection' to establish a connection), Trader, Addressable, Kaminari::ConfigurationMethods, Kaminari::ActiveRecordModelExtension, Client::GeneratedAssociationMethods, #<#<Class:0xba3b1c70>:0xba3b1d24>, ActiveRecord::Base, Kaminari::ActiveRecordExtension, GlobalID::Identification, ActiveRecord::Store, ActiveRecord::Serialization, ActiveModel::Serializers::Xml, ActiveModel::Serializers::JSON, ActiveModel::Serialization, ActiveRecord::Reflection, ActiveRecord::NoTouching, ActiveRecord::Transactions, ActiveRecord::Aggregations, ActiveRecord::NestedAttributes, ActiveRecord::AutosaveAssociation, ActiveModel::SecurePassword, ActiveRecord::Associations, ActiveRecord::Timestamp, ActiveModel::Validations::Callbacks, ActiveRecord::Callbacks, ActiveRecord::AttributeMethods::Serialization, ActiveRecord::AttributeMethods::Dirty, ActiveModel::Dirty, ActiveRecord::AttributeMethods::TimeZoneConversion, ActiveRecord::AttributeMethods::PrimaryKey, ActiveRecord::AttributeMethods::Query, ActiveRecord::AttributeMethods::BeforeTypeCast, ActiveRecord::AttributeMethods::Write, ActiveRecord::AttributeMethods::Read, ActiveRecord::Base::GeneratedAssociationMethods, #<#<Class:0xb8df1c14>:0xb8df1c50>, ActiveRecord::AttributeMethods, ActiveModel::AttributeMethods, ActiveRecord::Locking::Pessimistic, ActiveRecord::Locking::Optimistic, ActiveRecord::AttributeDecorators, ActiveRecord::Attributes, ActiveRecord::CounterCache, ActiveRecord::Validations, ActiveModel::Validations::HelperMethods, ActiveSupport::Callbacks, ActiveModel::Validations, ActiveRecord::Integration, ActiveModel::Conversion, ActiveRecord::AttributeAssignment, ActiveModel::ForbiddenAttributesProtection, ActiveRecord::Sanitization, ActiveRecord::Scoping::Named, ActiveRecord::Scoping::Default, ActiveRecord::Scoping, ActiveRecord::Inheritance, ActiveRecord::ModelSchema, ActiveRecord::ReadonlyAttributes, ActiveRecord::Persistence, ActiveRecord::Core, Object, PP::ObjectMixin, ActiveSupport::Dependencies::Loadable, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]

并且做Client.reflections.keys使出address现在开发中而不是在测试中。

4

1 回答 1

8

我知道了。问题是仅存在于我的测试环境(json-schema)中的addressablegem,在其依赖项之间具有 gem,即定义Addressable模块。因此,当 Rails 尝试自动加载时Addressable,它会找到定义的类addressable而不是我的关注类。所以,当然,该模块被包括在内,但它不是我想的那个。

更新我的 Gemfile时,自 2014 年 11 月以来json-schema已更新并且现在依赖于。addresable

很难抓住这个:)

于 2015-02-13T14:22:58.867 回答