Rails 奇怪的 NO.

robertyu · 2014年12月09日 · 最后由 huhongda 回复于 2014年12月09日 · 2029 次阅读

like语句中包含 like '%NO.%'

具体如下

没有问题: a = SchoolStar.where("id > ? and name like ?", SchoolStar::START_ID, "%NO.%")

存在问题: b = SchoolStar.includes(:school, :department, :images).where("id > ? and name like ?", SchoolStar::START_ID, "%NO.%")

奇怪的SQL语句:

SELECT `school_stars`.`id` AS t0_r0, `school_stars`.`name` AS t0_r1, `school_stars`.`weibo_name` AS t0_r2, `school_stars`.`grade` AS t0_r3, `school_stars`.`user_id` AS t0_r4, `school_stars`.`school_id` AS t0_r5, `school_stars`.`department_id` AS t0_r6, `school_stars`.`avatar` AS t0_r7, `school_stars`.`renren_name` AS t0_r8, `school_stars`.`check_state` AS t0_r9, `school_stars`.`favor_count` AS t0_r10, `school_stars`.`created_at` AS t0_r11, `school_stars`.`updated_at` AS t0_r12, `school_stars`.`applicant_method` AS t0_r13, `school_stars`.`sex` AS t0_r14, `school_stars`.`description` AS t0_r15, `school_stars`.`telephone` AS t0_r16, `school_stars`.`avatar_id` AS t0_r17, `school_stars`.`post_id` AS t0_r18, `school_stars`.`images_count` AS t0_r19, `school_stars`.`referrer_id` AS t0_r20, `school_stars`.`reason` AS t0_r21, `schools`.`id` AS t1_r0, `schools`.`name` AS t1_r1, `schools`.`created_at` AS t1_r2, `schools`.`updated_at` AS t1_r3, `schools`.`students_count` AS t1_r4, `schools`.`country` AS t1_r5, `schools`.`province` AS t1_r6, `schools`.`renren_school_id` AS t1_r7, `schools`.`visibility` AS t1_r8, `schools`.`category` AS t1_r9, `schools`.`courses_count` AS t1_r10, `schools`.`secret_posts_count` AS t1_r11, `schools`.`latitude` AS t1_r12, `schools`.`longitude` AS t1_r13, `schools`.`delta` AS t1_r14, `schools`.`city` AS t1_r15, `schools`.`members_count` AS t1_r16, `schools`.`undergraduate_count` AS t1_r17, `schools`.`level` AS t1_r18, `departments`.`id` AS t2_r0, `departments`.`name` AS t2_r1, `departments`.`school_id` AS t2_r2, `departments`.`created_at` AS t2_r3, `departments`.`updated_at` AS t2_r4, `departments`.`students_count` AS t2_r5, `departments`.`status` AS t2_r6, `departments`.`pinyin` AS t2_r7, `departments`.`delta` AS t2_r8, `images`.`id` AS t3_r0, `images`.`created_at` AS t3_r1, `images`.`updated_at` AS t3_r2, `images`.`image` AS t3_r3, `images`.`order` AS t3_r4, `images`.`deleted_at` AS t3_r5, `images`.`check_state` AS t3_r6, `images`.`imageable_id` AS t3_r7, `images`.`imageable_type` AS t3_r8 FROM `school_stars` LEFT OUTER JOIN `schools` ON `schools`.`id` = `school_stars`.`school_id` LEFT OUTER JOIN `departments` ON `departments`.`id` = `school_stars`.`department_id` LEFT OUTER JOIN `images` ON `images`.`imageable_id` = `school_stars`.`id` AND `images`.`imageable_type` = 'SchoolStar' WHERE (id > 0 and name like '%NO.%')

各位兄弟姐妹,有遇到过嘛?

共收到 11 条回复

includes 改成preload

#1楼 @huhongda 谢谢。可以说一下原理吗?

是 点号 引起的

原理有点复杂,引起的原因是:点号引起的!同上~

#3楼 @zhangyuan #4楼 @huhongda 是点引起的,但是没有NO也不会有问题,所以很奇怪。

includes自己推测要不要left join。 推测算法有bug? 新版本rails可能不会了。

#1楼 @huhongda @zhangyuan 这两位怎么这么屌啊

@317583395 你这里也不忘打你的广告~

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册