如何在 Ruby On Rails 中使用内连接 [英] How to use inner join in Ruby On Rails

查看:47
本文介绍了如何在 Ruby On Rails 中使用内连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究导轨.

我的需求是,

@accountUrl = Account.find_by_id(current_account_id)

@details = Detail.find_by_acc_id(@accountUrl.id)

如何编写上面例子中的内连接查询

How to write inner join query from above example

任何一个都可以.

推荐答案

在这个简单的情况下,Rails 不使用连接,它在代码中"连接:

In this simple case Rails does not use a join, it joins "in code":

Account.includes(:details).where(:id => current_account_id).first

它将进行两个单独的查询.

It will make two separate queries.

如果需要选择条件,则必须手动"(或通过范围)加入

If you need a condition for the select, you have to join "by hand" (or through a scope)

Account.joins(:details).where("details.name" => selected_detail).first

这将进行 INNER JOIN 并仅返回满足条件的帐户.

This will make an INNER JOIN and return only accounts satistfying the conditions.

这篇关于如何在 Ruby On Rails 中使用内连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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