activerecord 和 mongo / mongo-mapper 桥
我有一个使用 Active Record 的项目,我想使用 MongoDB 添加一些新功能。我如何将两个模型集成在一起,一个使用 MongoMapper,另一个使用 ActiveRecord (postgres),而不是重新发明轮子并重新编写整个网站。
我发现其他人已经成功完成,但没有示例:
例如,我有一个我想要将其与位置的现有 ActiveRecord 模型相关联的 STI Mongo 模型...即城市。以及基于 Authlogic 的用户模型...我如何才能协同使用它们?如果能在正确的方向上提供一两个指导,我将不胜感激。
谢谢,
I have a project which I have used Active Record and which I'd like to add some new features using MongoDB. Rather than re-invent the wheel and re-write my entire site, how can I integrate 2 models together, one which use MongoMapper and the other ActiveRecord (postgres).
I've found that others have done it successfully, but no examples:
For example, I have an STI Mongo model(s) of Places which I want to relate to an existing ActiveRecord model of Locations... ie Cities. and a User model based on Authlogic... how can I use them in concert? I'd be grateful for a pointer or two in the right direction.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这工作得很好,
地点模型
位置模型
This worked beautifully
places model
locations model
您还可以使用类型转换。
您不仅可以将 location_id 存储在 mongodb 中,还可以在 Location 类中实现类方法
from_mongo
和to_mongo
,以允许 mongomapper 序列化 mongo 安全中的每个 Location 实例-和友好的方式。简单(istc)示例:
地点模型
位置模型
当然,这与之前答案中的示例完全相同。这里很酷的一点是,您可以序列化完整的字符串,如果需要的话可以使用更多数据,从而更轻松地从 mongo 查询和检索数据。
例如,location_id 和坐标,因此您可以模仿 geodb 并映射/减少同一纬度的位置。 (愚蠢的例子,我知道)
参考:更多 MongoMapper Awesomenes< /a>
You can also use typecasting.
Instead of just storing the location_id in mongodb, you can implement to class methods,
from_mongo
andto_mongo
, in the Location class to allow mongomapper to serialize each Location instance in a mongo safe-and-friendly manner.Simple(istc) example:
Places model
Location model
This, of course, is exactly the same example as in the previous answer. The cool thing here is that you can serialize a full string, with more data if needed, thus making it easier to query and retrieve data from mongo.
For instance, the location_id and coordinates, so you can mimic a geodb and map/reduce for places in the same latitude. (stupid example, I know)
Reference: More MongoMapper Awesomenes