轨道4:如何使用包括()与其中()来检索关联对象 [英] Rails 4: How to use includes() with where() to retrieve associated objects

查看:76
本文介绍了轨道4:如何使用包括()与其中()来检索关联对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚用户如何将。凡()方法来检索相关的模型数据。在这个例子中,项目belongs_to的用户...

I can't figure out how to user the .where() method to retrieve associated model data. In this example, Projects belongs_to Users...

class Project < ActiveRecord::Base
    belongs_to :user
    has_many :videos
end

class User < ActiveRecord::Base
    has_many :projects
end

class ProjectsController < ApplicationController
  def invite
    @project = Project.includes([:user]).where( {:hashed_id=>params[:id]} ).first
  end

在应用程序/视图/项目/ invite.html.erg &LT;%=调试(@project)%GT; 收益:

In App/views/projects/invite.html.erg <%= debug( @project ) %> returns:

--- !ruby/object:Project
attributes:
  id: 22
  name: Some Project Name
  belongs_to: 1
  instructions: Bla bla bla
  active: true
  max_duration: 2
  max_videos: 
  created_at: 2013-08-26 15:56:50.000000000 Z
  updated_at: 2013-08-26 15:56:50.000000000 Z
  hashed_id: '1377532589'

如果不是关联的用户散列/阵列包括在本?我知道我可以手动调用添加第二个找到 / ,其中 @project。用户= User.where({:ID =&GT; @ project.belongs_to} ),但感觉并不像Rails的路是什么

Shouldn't the associated User hash/array be included in this? I know I could manually add it by calling a second find/where ( @project.user = User.where( {:id=>@project.belongs_to} ) but this doesn't feel like "The Rails Way". What is?

解决方案
我最初的问题是不正确的假设,即下制定的调试()将返回关联的对象(这个工程在CakePHP,因为它捆绑万事成数组)。

Solution My initial question was formulated under the incorrect assumption that debug() would return associated objects (this works in cakePHP because it bundles everything into arrays).

所以,我原来的code的的工作。不过,我已经正确命名表中提出的外键。我被看迁移方法混淆 t.belongs_to (自动创建正确命名foreign_key领域的的一场名为belongs_to的) 。所以我也只好列重命名为 USER_ID ,现在它就像在下面@ Veraticus的回答。

So my original code should work. However, I had incorrectly named the foreign key filed in the table. I got confused by looking at the migration method t.belongs_to (which automatically creates the correctly named foreign_key field, not a field named "belongs_to"). So I also had to rename that column to user_id and now it works just as described in @Veraticus's answer below.

推荐答案

用户对象不是项目的一部分对象,所以你将无法进行查看的项目:而是说 Project.includes(:用户),你告诉Rails的跃跃欲试-load引用的关联时,发现该项目。这样可以节省您的数据库调用的道路。例如,非急切:

The user object is not part of the project object, so you won't be able to view it on the project: rather, by saying Project.includes(:user), you're telling Rails to eager-load the referenced association when it finds the project. This saves you a database call down the road. For example, non-eagerly:

@project = Project.where(id: params[:id]).first # one database call, fetching the project
@project.user # another database call, fetching the user

和热切:

@project = Project.includes(:user).where(id: params[:id]).first # one database call, fetching both project and user
@project.user # no database interaction

这更重要与的has_many 查询哪里渴望加载协会可以节省N + 1数据库查询。

This matters more with has_many queries where eager-loading associations can save N+1 database queries.

您可以验证这一点是通过调用 @ project.user 在急于负荷后的一些点,检查你的日志的正确:你应该看到,没有数据库调用在这一点上。

You can verify this is working appropriately by calling @project.user at some point after the eager load and checking your logs: you should see that there was no database call at that point.

这篇关于轨道4:如何使用包括()与其中()来检索关联对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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