Rails引擎的模型关系Rails Gem [英] Rails Gem with model-relationships for Rails Engines
问题描述
我只是开发一个Rails应用程序,它将包含许多引擎。
然而,我无法编辑引擎内部的关系。
为了解决这个问题,我想创建一个关系-Gem,它将被包含在应用程序中并定义关系(请参阅: https://stackoverflow.com/a/11835899/603126 )。
假设我有一个User(名称空间隔离)引擎和一个Comment(名称空间隔离)引擎。
我想要的是重写/扩展关系中的关系-Gem,它将分享关系。
所以我添加了一个文件/ app / models / comment.rb与这些行(关系 - 宝石):
class Comment< CommentEngine :: Comment
belongs_to:user
end
class User< UserEngine :: User
has_many:comments
end
如果我运行我的rails申请,关系不会建立。
我错过了什么?
如何实现这一点?
非常感谢您提前
,所以我找到了一个解决方案。
你可以用装饰器猴子修改你的引擎(你需要把它放到config / initializers / initializer_name.rb中)
不知道这是否是件好事,但它的工作原理类似于魅力......
缺点是每次更改猴子补丁时都必须重新启动服务器......
编辑:看来这个猴子补丁会在收到几个请求后被垃圾回收。
编辑2:
这个后帮我出
如何在Ra中获取自动加载的猴子补丁代码ils?
您需要添加轻量级的Intializer以强制Rails为每个请求重新加载您的修补程序
Rails.configuration.to_prepare do
I'm just developing a Rails application which will have many Engines. However, I'm not able to edit relationships inside the Engines.
To solve this issue, I want to create a relationships-Gem which will be included in the Application and defines the relationships (see: https://stackoverflow.com/a/11835899/603126).
Let's assume, I have a User (namespaced and isolated) Engine and a Comment (namespaced and isolated) Engine. What I want is to override / extend the relationships inside the relationships-Gem which will share the relationships.
So I added a file /app/models/comment.rb with these lines (to the relationships-Gem):
class Comment < CommentEngine::Comment
belongs_to :user
end
class User < UserEngine::User
has_many :comments
end
If I run my rails application, the relationships won't be established.
What am I missing? How can this be achieved?
Thank you very much in advance
Ok, so I've found a solution for that.
You can just monkey-patch your Engine with Decorators (you need to put it into config/initializers/initializer_name.rb)
see: Extending a ruby gem in Rails
Don't know if this is a good thing, but it works like a charm...
The downside is that you have to restart the server every single time you make a change to the monkey-patching...
EDIT: It seems that this monkey-patch will be garbage collected after a few requests.
EDIT 2: This post helped me out How to monkey-patch code that gets auto-loaded in Rails? you need to add thin sin your Intializer to force rails to reload your patch for every request
Rails.configuration.to_prepare do
这篇关于Rails引擎的模型关系Rails Gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!