Rails 3 Joins -- 只选择某些列 [英] Rails 3 Joins -- Select only certain columns

查看:16
本文介绍了Rails 3 Joins -- 只选择某些列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是评论和用户之间的关系.每条评论都有一个用户,所以我在下面的代码中构建了一个连接.

Below is a relationship between Comments and a user. Each comment has one user so I'm building out a join in the code below.

我想知道如何构建此代码以仅在联接中包含特定列.我不需要所有的用户信息.只是名字.任何建议.

I was wondering how to build this code to only include specific columns in the join. I don't need all of the user information. Just the first_name. Any suggestions.

当前代码:

@comments = Comment.where(:study_id => @study.id).joins(:user)

推荐答案

你可以这样使用:

@comments = Comment.joins(:user)
                   .select("comments.*, users.first_name")
                   .where(study_id: @study.id)

这篇关于Rails 3 Joins -- 只选择某些列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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