Rails 4.2 无法识别我的模型问题 [英] Rails 4.2 will not recognize my model concerns

查看:46
本文介绍了Rails 4.2 无法识别我的模型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个版本有什么不同!在我的 Rails 3.2 应用程序中,我使用了关注点,它们是这样设置的:<代码>- 应用程序- 楷模类别.rb产品.rb--顾虑- 类别第三方_a.rb第三方_b.rb- 产品第三方_a.rb第三方_b.rb- 仓库第三方_a.rb第三方_b.rb

What a difference a version makes! In my Rails 3.2 application, I was using concerns, and they were set up like so: --app --models category.rb product.rb --concerns --category third_party_a.rb third_party_b.rb --product third_party_a.rb third_party_b.rb --warehouse third_party_a.rb third_party_b.rb

我担心名称相同,命名空间位于不同目录中.这就是我被教导这样做的方式,它比 Rails 4.2 默认将一堆 Ruby 文件集中在一个 concerns 目录中更有意义.如果我有多个对象需要与多个第三方 API 交互,那么这是最好的方法.

I have concerns with the same name, namespaced into different directories. It was how I was taught to do it, and it makes a lot better sense than the Rails 4.2 default of lumping a bunch of Ruby files in a concerns directory. If I have multiple objects that need to interact with multiple third party APIs, then this is the very best way.

但是将这个应用程序升级到 Rails 4.2,好吧,它拒绝工作.我收到了很多很多关于这些问题的投诉:

But upgrading this application to Rails 4.2, well, it refuses to work. I'm getting lots, and lots, and lots of complaints on the concerns:

Unable to autoload constant Category::ThirdPartyA

它期望文件定义它"或其他东西.该文件如下所示:

it "expected the file to define it" or something. The file looks like this:

module EbayInteraction

  extend ActiveSupport::Concern

  module ClassMethods
  ...

不,那里没有其他命名空间.但是 Rails 3.2 不需要它.它完美无缺.

No, there's no other namespace in there. But Rails 3.2 didn't require it. And it worked flawlessly.

所以我进入文件并将声明更改为:

So I go into the file and change the declaration to:

module Category::ThirdPartyA

我不知道这是否正确.我对模块和命名空间感到非常困惑,这两个概念对我来说都没有任何意义.

I have no idea if that is correct. I am very confused by modules and namespaces, and neither concept makes any sense at all to me.

然后我进入模型本身并include关注这样的问题:

And then I go into the model itself and include the concern like this:

include Category::ThirdPartyA

我克服了错误.然后抱怨ThirdPartyB,所以我也这样做.等等.

And I get past the error. Then complains about ThirdPartyB, so I do the same. And so on.

当我完成后,我现在收到此投诉:

When I'm finished, I now get this complaint:

Circular dependency detected while autoloading constant Category::ThirdPartyA

这是一个愚蠢且具有误导性的错误.这种担忧只有一种苗条的方法.不可能有任何循环.

That is a silly and misleading error. That concern has exactly one slim method. No possibility of anything circular.

现在我卡住了.我唯一的选择是重命名我的所有文件并将它们集中到关注目录中.我真的不想这样做,它不可维护或可扩展.这是怎么回事?

Now I am stuck. My only alternative is to rename all of my files and lump them into the concerns directory. I really don't want to do that, it's not maintainable or extensible. What is going on here?

推荐答案

我已决定不浪费任何生产时间来尝试修复 4.2 中出色的命名空间问题.核心团队似乎再次改进"了一个工作约定.

I have decided against wasting any productive time with trying to fix my excellently-namespaced concerns in 4.2. The core team appears to have "improved" a working piece of convention once again.

  • 我在所有关注文件名前面加上模型的名称,其中每个文件名都是 included.并相应地更改了模块名称.

  • I prepended all of the concern filenames with the name of the model in which each is included. And changed the module name accordingly.

我将我所有关注的问题从他们的模型子目录上移到了关注的级别.

I moved all of my concerns from their model subdirectory up a level to be in concerns.

现在一切都恢复了,尽管它看起来令人作呕和恶心.无论如何,担心都是愚蠢的.只是将胖模型内容放入不同文件的一种方法.我正在移动这个应用程序以使用 Trailblazer gem 和架构.这将使我免受 Rails 5 中引入的任何愚蠢行为的影响,并且是我将来坚持使用该平台的唯一方法.

Now everything works again, even though it is disgusting and gross-looking. Concerns are stupid anyway. Just a way to put fat model stuff into a different file. I'm moving this app to use the Trailblazer gem and architecture. That will insulate me from any silliness that's introduced in Rails 5, and is the only way I'll stick with the platform in the future.

这篇关于Rails 4.2 无法识别我的模型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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