扶手:查找所有相关对象父对象 [英] Rails: Finding all associated objects to a parent object

查看:126
本文介绍了扶手:查找所有相关对象父对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创造了铁轨一个复杂的对象与原则父对象恢复它为每个部分(objective_section,contact_section等)的一些子对象,是有办法,我可以获取所有相关对象的父对象恢复?

I have created a complex object in rails with a principle parent object "Resume" it has a number of child objects for each section("objective_section", "contact_section", etc), is there a way I can fetch all associated objects to the parent object Resume?

推荐答案

如果通过抓取你的意思是从数据库负载都在同一个查询,则肯定的:

If by fetch you mean load from the database all in one query, then sure:

Resume.first(:include => [:objective_sections, :contact_sections]) # etc...

如果这是一个常见的​​模式,你要干的事情了没有太多的精力,你可以在你的模型扔到named_scope这样的:

If this is a common pattern and you want to DRY things up without much effort, you can throw this into a named_scope in your model:

class Resume < ActiveRecord::Base
  has_many :objective_sections
  has_many :contact_sections

  named_scope :with_sections, :include => [:objective_sections, :contact_sections]
end

这篇关于扶手:查找所有相关对象父对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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