如何在 Ruby On Rails 中使用内连接

编程入门 行业动态 更新时间:2024-10-27 17:18:27
本文介绍了如何在 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 中使用内连接

本文发布于:2023-10-29 02:46:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538503.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Ruby   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!