2010-02-13 61 views
1

我有一个使用Active Record的项目,我想使用MongoDB添加一些新功能。我没有重新发明轮子并重写我的整个网站,我怎样才能将2个模型集成在一起,其中一个使用MongoMapper和另一个ActiveRecord(postgres)。activerecord和mongo/mongo-mapper bridge

我发现,别人已经成功地做到了,但没有例子:

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

例如,我有地方的STI蒙戈模型(一个或多个),我想涉及到现有的位置...即城市的ActiveRecord模型。和一个基于Authlogic的用户模型......我如何在音乐会中使用它们?我会很感激一个或两个在正确方向的指针。

感谢,

回答

2

这美丽的工作

地方模型

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 
1

您还可以使用类型转换。

而不是仅仅存储在MongoDB中的LOCATION_ID,可以实现类的方法,from_mongoto_mongo,在Location类,允许mongomapper连载每个位置例如在蒙戈安全和友好的方式。

简单(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和地图/减少在同一纬度的地方。 (我知道的愚蠢的例子)

参考:More MongoMapper Awesomenes