奇怪的LEFT OUTER JOIN上包括轨道3的渴望装载 [英] Weird LEFT OUTER JOIN on Includes eager loading of rails 3

查看:124
本文介绍了奇怪的LEFT OUTER JOIN上包括轨道3的渴望装载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道它是一个活动记录错误或不。 或者是有办法做到.includes和禁用LEFT OUTER JOIN明确的战略。 这是我的情况:

I'm not sure it is a active record bug or not. Or is there a way to do .includes and disable the LEFT OUTER JOIN strategy explicitly. Here is my cases:

由于关键字是'ABC'没有一个点

Given the keyword is 'abc' without a 'dot'

Post.where(:name => "abc").includes(:author)

有用作一般两sql语句

There are two sqls used as normal

Post Load (0.8ms)  SELECT `posts`.* FROM `posts` WHERE `posts`.`name` = 'abc'
Author Load (0.4ms)  SELECT `authors`.* FROM `authors` WHERE `authors`.`id` IN (1)

由于关键字是ABC。以点

Given the keyword is 'abc.' with a 'dot'

Post.where(:name => "abc.").includes(:author)

在SQL中使用LEFT OUTER JOIN策略,这是令人困惑的。

The sql is using LEFT OUTER JOIN strategy, that's confusing.

SELECT `posts`.`id` AS t0_r0, `posts`.`name` AS t0_r1, `posts`.`author_id` AS t0_r2, `posts`.`created_at` AS t0_r3, `posts`.`updated_at` AS t0_r4, `authors`.`id` AS t1_r0, `authors`.`created_at` AS t1_r1, `authors`.`updated_at` AS t1_r2 
FROM `posts` LEFT OUTER JOIN `authors` ON `authors`.`id` = `posts`.`author_id` 
WHERE `posts`.`name` = 'abc.'

我知道预先加载与包括实现与LEFT OUTER JOIN策略时,有对联想的条件,如

I know eager loading with includes is realized with LEFT OUTER JOIN strategy when there are conditions on the association, like

Post.includes(:author).where(:authors => {:name => 'zhougn' })

但是,在我的测试的情况下,不存在这样的一个条件。基本上,无论多SQL战略和LEFT OUTER JOIN策略可以给我正确的结果,但如果帖子与作者都存储在不同的数据库中,LEFT OUTER JOIN策略会失败。

But in my test cases, there is no such a condition. Basically both Multi-SQL strategy and LEFT OUTER JOIN strategy can give me correct result, but when Posts and Authors are stored in different databases, LEFT OUTER JOIN strategy will fail.

推荐答案

是的,这是一个错误。问题#950: ActiveRecord的查询更改时,点/周期为条件的价值。固定在轨道4,它的出现,由德precating被越来越挂了这个(除了做不同的,需要程序员是明确的,当他们需要一个左外连接无解的功能 - 以及该表可以使用字符串条件时包括)引用。

Yes, this is a bug. Issue #950: ActiveRecord query changing when a dot/period is in condition value. Fixed in Rails 4, it appears, by deprecating the feature that was getting hung up on this (no solution except to do it different and require the programmer to be explicit when they want a left outer join -- as well as which tables are referenced when using string conditions with includes).

我目前的肮脏的解决方法(在Rails 3中),是连接Ç这样的点code /德$ C $:

My current dirty workaround (in Rails 3) is to encode/decode the dots like this:

.where("posts.name = REPLACE(?, '˙', '.')", somename.gsub(".", "˙"))

这篇关于奇怪的LEFT OUTER JOIN上包括轨道3的渴望装载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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