`
xu_wccq
  • 浏览: 129005 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

使用include中嵌Hash取出一个多层次的对象关联数据

    博客分类:
  • ruby
阅读更多
使用include中嵌Hash取出一个多层次的对象关联数据.

首先有如下关系:
project issue  : 一对多
issue comment : 一对多
comment history : 一对多
Project [1] <---- [n] issue (1) <---- (n) comment [1] <---- [n] history


class Project
  has_many :issues
end


class Issue
  belongs_to :project
  has_many :cccc      # cccc  为了区别后面的名称不致混淆

#在issue中创建一条与之对应的comment,这里用实例变量为了方便在issue保存的时候也一同保存comment的信息。
  def init_comment(content = "")
    @current_comment ||= Comment.new(:resource => self, :creater => current_user, :content => content)
    @issue_before_change = self.clone
    @current_comment
  end
  
#issue保存的时候一同保存comment,以及histories的信息。
##这里需要注意的是,要保证histories表中的comment_id(histories表中与comment关联的外键) `comment_id` int(11) default '0', 必需设置一个默认值,否则将不能保存comment 和 history的信息。
  def before_save
    if @current_comment
      # attributes changes
      (Issue.column_names - %w(id content resource_id resource_type)).each {|c|
        @current_comment.histories << History.new(:property=>c,
                                                  :comment => @current_comment,
                                                  :old_value => @issue_before_change.send(c),
                                                  :value => send(c)) unless send(c)==@issue_before_change.send(c)
      }
      @current_comment.save
    end
    # Save the issue even if the comment is not saved (because empty)
    true
  end

end


class Comment
  belongs_to :issue
  has_many :histories
end


class History
  belongs_to :comment
end


一条语句实现取出这其中的所有记录:
Project.find(:first,:include =>[{ :issue => { :cccc => { :histories => :comment }}}]
注意,include中的关联的名称要对应好。


API 中的相关使用介绍
To include a deep hierarchy of associations, use a hash:

  for post in Post.find(:all, :include => [ :author, { :comments => { :author => :gravatar } } ])

That‘ll grab not only all the comments but all their authors and gravatar pictures. You can mix and match symbols, arrays and hashes in any combination to describe the associations you want to load.

All of this power shouldn‘t fool you into thinking that you can pull out huge amounts of data with no performance penalty just because you‘ve reduced the number of queries. The database still needs to send all the data to Active Record and it still needs to be processed. So it‘s no catch-all for performance problems, but it‘s a great way to cut down on the number of queries in a situation as the one described above.

Since the eager loading pulls from multiple tables, you‘ll have to disambiguate any column references in both conditions and orders. So :order => "posts.id DESC" will work while :order => "id DESC" will not. Because eager loading generates the SELECT statement too, the :select option is ignored.

You can use eager loading on multiple associations from the same table, but you cannot use those associations in orders and conditions as there is currently not any way to disambiguate them. Eager loading will not pull additional attributes on join tables, so "rich associations" with has_and_belongs_to_many are not a good fit for eager loading.

When eager loaded, conditions are interpolated in the context of the model class, not the model instance. Conditions are lazily interpolated before the actual model exists. 

分享到:
评论

相关推荐

    论文研究-基于数据关联性聚类的数据布局算法.pdf

    现代信息系统的突出特征是...建立了数据和数据的关联矩阵,基于关联矩阵进行聚类,再将数据分配到各个数据中心中,计算执行应用时的数据迁移量,并与一致hash算法进行了比较,结果表明数据迁移量大大低于一致hash算法。

    MD5校验工具Hash.exe使用说明.pdf

    Hash.exe 是一个 MD5 校验工具,由浪潮电子信息产业股份有限公司开发,用于确保发布的 FW 版本及下载使用版本的一致性。本文档将详细介绍 Hash.exe 工具的使用说明和详细步骤。 一、Hash.exe 工具概述 Hash.exe ...

    数据库中如何用Hash关键字提高数据库性能

    应用称为hash关键字(引用单独一个hash)或hash桶(一个hash关键字集合)的字符串目录的优秀方法可大大节省磁盘空间并提高性能。hash是应用一个指定字符串算法的整数结果。有各式各样的hash算法,但最常用的是内置的...

    hash树的建立和在数据挖掘中的应用

    hash树建立的过程,hash树在关联规则的发现过程的应用。

    使用Hash散列从海量IP地址中查找IP地址

    一个简单的使用hash来实现从海量IP地址中查询是否存在待查找的IP地址。主要特点有: (1)使用批处理,一键自动编译,处理;可直接运行。 (2)完美的展示了hash在查询中的使用方法。

    Hashcat使用教程

    目前GPU的速度越来越快,使用GPU超强的运算速度进行暴力密码破解也大大提高了成功率,曾经看到老外用26块显卡组成的分布式破解神器让我羡慕 不已。要说目前最好的GPU破解HASH的软件,非HashCat莫属了。下面我就为...

    利用Hash技术统计C源程序中关键字的频度

    扫描一个C源程序,用Hash表存储该程序中出现的关键字,并统计该程序中的关键字出现的度。用线性探测法解决Hash冲突。设Hash函数为:Hash(Key)=[(Key的首字母序号)*100+(Key的尾字母序号)] Mod 41。关键字39个,参考...

    Hash表的分析以及组成原理解析及代码实现.md

    Hash表采用了数组加链表的结构,即一个数组元组中不再是存储单个元素,...从小往大看,每一个节点代表一个对象,并采用单链表的形式将每个节点串联起来,因此要先创建一个节点,该节点用于存储信息以及关联下一个节点

    win8以上后缀关联userchoice计算hash值

    实现win8以上系统设置默认文件的后缀关联。自动计算userchoice的hash值

    Redis笔记整理-五中数据类型之String和Hash

    Redis笔记整理-五中数据类型之String和Hash,这两种数据类型是我们常用语做缓存,从而减轻数据库的压力,缓存我们一般放到服务成,被多个表现成调用达到公用性

    aHash 是一种使用 AES 硬件指令的非加密哈希算法_rust_代码_下载

    这也避免了通过从一个映射读取并写入另一个映射来意外的二次行为。 目标和非目标 AHash的输出没有固定的标准。这允许它随着时间的推移而改进。例如,如果发现任何更快的算法,aHash 将被更新以合并该技术。同样,...

    地理坐标 GEOHASH示例代码 geohash.zip

    项目中使用的 GEOhash 算法, 在网上公开的GEOhash demo基础上, 做了升级, 功能: 1. 根据指定坐标生成 GEOhash对象 2. 根据当前坐标(GEOhash对象)获取周边8/9个GEOhash对象 3. [升级]根据当前坐标获取指定半径...

    数据挖掘考试题目——关联分析分享.pdf

    本资源摘要信息涵盖了数据挖掘考试题目中的关联分析部分,总共包括选择题、填空题、判断题和简答题四个部分。下面我们将对每个部分的知识点进行详细的解释和分析。 一、选择题 本部分包括10个选择题,涵盖了关联...

    非常使用的 基于geohash 找最近位置java代码

    非常使用的 基于geohash 找一定范围内的 最近位置java代码

    最快的排序算法 最快的内容查找算法-----暴雪的Hash算法,排序算法数据结构

    Hash算法在数据结构中的应用非常广泛,例如在数据库中使用 Hash索引来快速查找数据,在编程语言中使用 Hash 表来实现快速的字符串查找等。 Hash算法的优点是速度快、效率高,但同时也存在着 collisions 的问题,需要...

    mysql_hash.exe/使用hash登陆mysql

    在获取到mysql用户的hash后, 可用hash直接登陆mysql进行操作 比如我们注入出数据库的hash,但是没办法拿到webshell 我们可以使用mysql_hash,用hash登陆并控制数据库 使用方法: mysql_hash.exe -u root -p Enter ...

    js中hash和ico的关联分析.docx

    js中hash和ico的关联分析.docx

    国内 精确到 乡级 的 经纬度 以及 geoHash 数据表

    数据更新于:2020年11月06日 ,包含4个json(省级、地级、县级、乡级),3个整理后的txt,1个java文件 带注释,具体可看文章:https://mp.csdn.net/editor/html/115234415

    Prototype使用指南之hash.js

    Hash对象(关联数组)是Prototype新建的一个对象,要创建一个Hash对象可以调用$H(object)方法,因为javascript本身的特点(对象本身就是关联数组) ,所以实现Hash也很简单,Prototype中的Hash只是javascript的关联数组...

Global site tag (gtag.js) - Google Analytics