Rails 3 一个 MongoMapper 模型和一个/多个 Active Record 模型之间的多态关联
我有一个记录模型(Active Record),它存储一些自定义日志。
Record 与我的应用程序中的所有其他模型具有多态关联,我可以有效地记录我想要的内容,在其他控制器中挂钩我的 Record 方法。
我需要什么:
将日志保存在单独的数据库中。
所以我必须:
能够在我的应用程序中管理两个不同的数据库(一个是 Postgres/ActiveRecord,另一个是 MongoDB/MongoMapper)
在我的 Record 模型(现在使用 MongoMapper)和我的其余 Active Record 模型之间生成多态关联。
这样我就可以将日志保存到 MongoDB 数据库中。
谢谢。
I have a Record model (Active Record) that stores some custom logs.
Record is in polymorphic association with all the other model in my app, and I can effectively log what I want hooking my Record methods in the other controllers.
What I need:
To have the logs in a separate database.
So I have to:
Be able to manage two different databases in my apllication (one is Postgres/ActiveRecord and the other one is MongoDB/MongoMapper)
Generate a polymorphic association between my Record model, now with MongoMapper, and the rest of my Active Record models.
That way I can persist my logs to the MongoDB database.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可以做到的。
要创建多态关联,您需要类和 ID。通常,这些字段将命名为
_type
和_id
‡。您需要进行一些接线才能使一切正常工作。_type
和_id
创建一个 MongoMapper::Document 类(我相信 MongoMapper 允许Class
code> 作为密钥类型)以及您可能需要的任何其他密钥。定义方法=
和可能向 ActiveRecord 模型添加一个方法,允许它们访问你的 MongoMapper 日志记录类。如果有很多,您可能需要构建一个模块并将其包含在需要此类功能的所有类中。
‡ 替换为对您的应用程序有意义的内容,例如“参考”或“主题”
Yes this can be done.
To create a polymorphic association you need both the class and an id. Idiomatically the fields will be named
<assoc>_type
and<assoc>_id
‡. You will need to do some wiring up to make everything work.<assoc>_type
and<assoc>_id
with the correct types (I believe MongoMapper allowsClass
as a key type) along with any other keys you may need.Define the method
<assoc>
and<assoc>=
Possibly add a method to your ActiveRecord models allowing them to access you MongoMapper logging class. If there are a lot, you may want to build a module and include it in all the classes that need that kind of functionality.
‡ replace with something meaningful for you application like 'reference' or 'subject'