我在 rails 上工作。
我的需要是
@accountUrl = Account.find_by_id(current_account_id)
@details = Detail.find_by_acc_id(@accountUrl.id)
如何从上面的示例编写内部联接查询
可以任何一个。
最佳答案
在这种简单的情况下,Rails不使用联接,而是“以代码形式”联接:
Account.includes(:details).where(:id => current_account_id).first
它将进行两个单独的查询。
如果需要选择条件,则必须“手动”(或通过作用域)加入
Account.joins(:details).where("details.name" => selected_detail).first
这将创建一个INNER JOIN,并且仅返回满足条件的帐户。
关于ruby-on-rails - 如何在Ruby On Rails中使用内部联接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12702506/