解决 Rails 应用程序中的类名冲突
我一直在构建一个执行会计功能的 Rails 应用程序。作为其中的一部分,我有一个类名为 Transaction
的模型。到目前为止一切顺利,我已经构建此功能大约一个月了,一切都按预期工作。
到目前为止...
我刚刚发现一些几个月前使用 Ruport 库开发的旧报告功能已停止工作。看来 Ruport 在生成 PDF 时需要一个包含名为 Transaction
的类/模块的库。
TypeError in Admin/team reportsController#generate
Transaction is not a module
...
This error occurred while loading the following files:
pdf/writer
transaction/simple
所以,我在这里寻找快速修复方法。希望不涉及重命名我的 Transaction
模型并重构过去几周的代码。
期待一些聪明的建议:)
I have been building a Rails application that performs accounting functionality. As part of this, I have a model with the class name Transaction
. So far so good, I have been building this functionality for a month or so, and everything is working as expected.
Until now...
I have just discovered some older reporting functionality that was developed months ago using the Ruport library has stopped working. It appears that Ruport, when generating PDFs, requires a library that also has a class/module named Transaction
.
TypeError in Admin/team reportsController#generate
Transaction is not a module
...
This error occurred while loading the following files:
pdf/writer
transaction/simple
So, I'm looking for a quick fix here. One that hopefully doesn't involve renaming my Transaction
model and refactoring the last few weeks worth of code.
Looking forward to some clever suggestions :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
已经回答了并且很旧,但我来到这里遇到了同样的问题,但以不同的方式解决了它。
我有两个名为 Pull 和 Query 的模型。
尝试在 Pull 中的方法中引用 Query.some_static_method() 会导致查询解析为
ActiveRecord::AttributeMethods::Query:Module
。通过使用
::Query.some_static_method()
将空名称空间放在其前面来解决此问题Already answered and old, but I came here with the same problem, but solved it in a different way.
I have two Models named Pull and Query.
Trying to reference
Query.some_static_method()
within a method in Pull resulted in Query resolving toActiveRecord::AttributeMethods::Query:Module
.Solved it by putting the empty namespace in front of it with
::Query.some_static_method()
我认为问题在于 Ruport 需要 PDF::Writer gem,而后者又需要定义模块 Transaction 的 Transaction::Simple gem。
ActiveRecord 中肯定有 #transaction 方法,但我不认为 Rails 中有 Transaction 模块或类。我很乐意在这一点上得到纠正。
命名空间通常是避免此类命名冲突的最佳实践。例如
,但是,命名空间 ActiveRecord 模型可能会引发其他问题。
尽管可能很耗时,但重命名事务模型可能是最好的选择。
如果需要,您仍然可以保留现有的事务数据库表,这样您的迁移就不需要更改,只需将
self.table_name = "transactions"
放入模型中即可。您与其他模型的关联仍然可以通过在关联调用中指定 class_name 来命名为“事务”。例如,
这两个建议可能会也可能不会为您节省一些时间。
I believe the issue is down to Ruport requiring the PDF::Writer gem, which in turn requires the Transaction::Simple gem which defines the module Transaction.
There is certainly a #transaction method in ActiveRecord, but I do not think there is a Transaction module or class within Rails. I'll be happy to be corrected on that one.
Namespacing is usually the best practice for avoiding naming conflicts like this. E.g.
However, namespacing ActiveRecord models may throw up other issues.
As time consuming as it may be, renaming your Transaction model may be the best bet.
You can still keep your existing transactions database table if you wanted, so your migrations don't need to change, by putting
self.table_name = "transactions"
inside your model.Your associations with other models can also still be named "transaction(s)" by specifying the class_name in your association call. E.g.
Those two suggestions may or may not save you some time.
你的问题可能来自于 Transaction 也是 Rails 中的保留字......
Your problem may come from the fact that Transaction is also a reserved word in Rails…