有没有一种方法,以确保一个ActiveRecord层次每个记录一个对象引用? [英] Is there a way to ensure one object reference per record in an ActiveRecord hierarchy?

查看:126
本文介绍了有没有一种方法,以确保一个ActiveRecord层次每个记录一个对象引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,当我抓住一些层次ActiveRecord的结构,有相当多的命中数据库。包括选项,以充实尽可能多的结构可能的:我使用了改进这一点。即便如此,似乎ActiveRecord的不映射的关系(如父子)与达到创纪录的唯一参考的倒数两侧。也就是说,我们得到了一个导航的ActiveRecord的结构,而是一个以我的知识并不能保证给定记录的唯一副本。

It seems when I grab some hierarchical ActiveRecord structure that there are quite a few hits to the database. I improved this using the :include option to flesh out as much of the structure as possible. Even so, it seems that ActiveRecord does not map the reciprocal sides of a relationship (e.g. parent-child) with unique references to a record. That is, we get a navigable ActiveRecord structure, but one that to my knowledge doesn't guarantee a unique copy of a given record.

node = Node.find(1, :include => {:document => :node})
node.object_id #A
node.document.node.object_id #B although I expect/desire A

不可避免的ActiveRecord?有其他人提出的,因为这样的缺点,替代奥姆斯? (不要误会我的意思:。ActiveRecord的是一个伟大的工具,但是,所有的工具都有长处和短处)

Unavoidable in ActiveRecord? Have others moved to alternative ORMs because of shortcomings like this? (Don't get me wrong: ActiveRecord is a great tool. But all tools have strengths and weaknesses.)

说我是写我自己的SQL查询(它甚至可以是一个存储过程)返回一个包含所有相关数据为我的ActiveRecord层次多个结果集(每个表一个)。有没有无痛的方式,我可以明确地映射关联,而无需ActiveRecord的弄错了试图创建新的关联我明确的映射?

Say I were to write my own SQL query (it could even be a stored proc) returning multiple result sets (one per table) containing all the relevant data for my ActiveRecord hierarchy. Is there a painless way that I can explicitly map the associations without having ActiveRecord mistaking my explicit mappings for an attempt to create new associations?

推荐答案

我根本不认为ActiveRecord的设计为支持这种想法。它只是意味着你有ActiveRecord的工作,了解其利弊。因为我已经找到了解决此问题。

I simply don't think ActiveRecord is designed to support this idea. It simply means you have to work with ActiveRecord, understanding its pros and cons. I've since found ways to work around this issue.

我看着DataMapper的,我看到它确实 specifially地址此问题:

I've looked at DataMapper and I see that it does specifially address this issue:

在数据库中的一行   应该等于一个对象引用。   pretty的简单的想法。 pretty的深刻   影响。如果您运行下面的code   在ActiveRecord的,你会看到全是假的   结果。在DataMapper的做同样的   这是真的一路下跌......

One row in the database should equal one object reference. Pretty simple idea. Pretty profound impact. If you run the following code in ActiveRecord you’ll see all false results. Do the same in DataMapper and it’s true all the way down...

这篇关于有没有一种方法,以确保一个ActiveRecord层次每个记录一个对象引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆