activerecord 和 mongo / mongo-mapper 桥

发布于 2024-08-20 23:28:17 字数 634 浏览 6 评论 0原文

我有一个使用 Active Record 的项目,我想使用 MongoDB 添加一些新功能。我如何将两个模型集成在一起,一个使用 MongoMapper,另一个使用 ActiveRecord (postgres),而不是重新发明轮子并重新编写整个网站。

我发现其他人已经成功完成,但没有示例:

http://groups.google.com/group/mongomapper/browse_thread/thread/ec5ad00e18e7dd2c/887b8b0b904a8f73?lnk=gst&q=activerecord#887b8b0b904a8f73

例如,我有一个我想要将其与位置的现有 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:

http://groups.google.com/group/mongomapper/browse_thread/thread/ec5ad00e18e7dd2c/887b8b0b904a8f73?lnk=gst&q=activerecord#887b8b0b904a8f73

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2024-08-27 23:28:17

这工作得很好,

地点模型

  key :location_id, Integer, :required => true

    def location
        Location.find(location_id)
    end

位置模型

  def self.find_places(id)
    Property.find_by_location_id(id)
  end

  def find_places
    Property.find_by_location_id(id)
  end

This worked beautifully

places model

  key :location_id, Integer, :required => true

    def location
        Location.find(location_id)
    end

locations model

  def self.find_places(id)
    Property.find_by_location_id(id)
  end

  def find_places
    Property.find_by_location_id(id)
  end
自由如风 2024-08-27 23:28:17

您还可以使用类型转换。

您不仅可以将 location_id 存储在 mongodb 中,还可以在 Location 类中实现类方法 from_mongoto_mongo,以允许 mongomapper 序列化 mongo 安全中的每个 Location 实例-和友好的方式。

简单(istc)示例:

地点模型

key :location, Location, :required => true

位置模型

def self.to_mongo(location)
  location[:id]
end

def self.from_mongo(location_id)
  find(location_id)
end

当然,这与之前答案中的示例完全相同。这里很酷的一点是,您可以序列化完整的字符串,如果需要的话可以使用更多数据,从而更轻松地从 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 and to_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

key :location, Location, :required => true

Location model

def self.to_mongo(location)
  location[:id]
end

def self.from_mongo(location_id)
  find(location_id)
end

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文