本文介绍了如何在 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 中使用内连接
发布评论