本文介绍了Rails 3的关联是1 MongoMapper模型和一个/多个活动记录模式/秒之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储一些自定义日志记录模式(Active Record的)。

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.

我需要的:

要具备日志在一个单独的数据库。

To have the logs in a separate database.

所以,我必须:

  1. 能够管理在我apllication两个不同的数据库(一个是Postgres的/ ActiveRecord的,另一种是MongoDB中/ MongoMapper)

  1. Be able to manage two different databases in my apllication (one is Postgres/ActiveRecord and the other one is MongoDB/MongoMapper)

生成我的活动记录模式,其余我的记录模式之间的关联是,现在MongoMapper和。

Generate a polymorphic association between my Record model, now with MongoMapper, and the rest of my Active Record models.

这样我可以坚持我的日志到MongoDB的数据库。

That way I can persist my logs to the MongoDB database.

感谢。

推荐答案

是可以做到这一点。

要创建你需要两个类和ID的多态关联。惯用的领域将被命名为< assoc命令> _type < assoc命令> _id . You will need to do some wiring up to make everything work.

  1. 创建一个MongoMapper ::文档类的钥匙< assoc命令> _type < assoc命令> _id 用正确的类型(我相信MongoMapper允许作为一键式)以及任何其它键,你可能需要。
  2. 定义方法< assoc命令> < assoc命令> =

  1. Create a MongoMapper::Document Class with the keys <assoc>_type and <assoc>_id with the correct types (I believe MongoMapper allows Class as a key type) along with any other keys you may need.
  2. Define the method <assoc> and <assoc>=

def assoc
  assoc_type.find(assoc_id)
end

def assoc=(input)
   assoc_type = input.class #STI makes this more complicated we must store the base class
   asspc_id = input.id
end

  • 可能的方法添加到您的ActiveRecord模型允许他们访问你MongoMapper日志类。如果有很多,你可能想建立一个模块,并将其包含在所有需要那种功能的类。

  • 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.

    ‡替换像参考或主体

    这篇关于Rails 3的关联是1 MongoMapper模型和一个/多个活动记录模式/秒之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 10-29 11:46