rails/rails — Issue #3551: Trouble with periods in queries with includes
Author: gunn
Created: 2011-11-07 12:25:10+00:00
State: open

```ruby
Song.where(:title => "a.b.c")
# SELECT "songs".* FROM "songs" WHERE "songs"."title" = 'a.b.c'
```
Good.

```ruby
Song.where(:title => "a.b").includes(:artist)
# SELECT "songs".* FROM "songs" WHERE "songs"."title" = 'a.b'
```
Good.

```ruby
Song.where(:title => "a.b.c").includes(:artist)
# SELECT "songs"."id" AS t0_r0, "songs"."title" AS t0_r1, "songs"."text" AS t0_r2, "songs"."rating" AS t0_r3, "songs"."view_count" AS t0_r4, "songs"."play_count" AS t0_r5, "songs"."published" AS t0_r6, "songs"."published_at" AS t0_r7, "songs"."created_at" AS t0_r8, "songs"."updated_at" AS t0_r9, "songs"."artist_id" AS t0_r10, "songs"."album_id" AS t0_r11, "songs"."image" AS t0_r12 <... snip ...> "artists"."alphabetic_title" AS t1_r14, "artists"."cached_slug" AS t1_r15, "artists"."title" AS t1_r16, "artists"."image_description" AS t1_r17, "artists"."search_index_column" AS t1_r18 FROM "songs" LEFT OUTER JOIN "artists" ON "artists"."id" = "songs"."artist_id" WHERE "songs"."title" = 'a.b.c'
```
What? Adding a second period has changed the query significantly.

Where this really causes trouble (that I've found) is when we are doing custom selects:

```ruby
Song.where(:title => "a.b").select("RANDOM() AS custom_field").order("custom_field").includes(:artist)
# SELECT RANDOM() AS custom_field FROM "songs" WHERE "songs"."title" = 'a.b' ORDER BY custom_field
```
Good.

```ruby
Song.where(:title => "a.b.c").select("RANDOM() AS custom_field").order("custom_field").includes(:artist)
# SELECT "songs"."id" AS t0_r0, "songs"."title" AS t0_r1, "songs"."text" AS t0_r2, "songs"."rating" AS t0_r3, "songs"."view_count" AS t0_r4, "songs"."play_count" AS t0_r5, "songs"."published" AS t0_r6, "songs"."published_at" AS t0_r7, "songs"."created_at" AS t0_r8, "songs"."updated_at" AS t0_r9, "songs"."artist_id" AS t0_r10, "songs"."album_id" AS t0_r11, "songs"."image" AS t0_r12 <... snip ...> "artists"."alphabetic_title" AS t1_r14, "artists"."cached_slug" AS t1_r15, "artists"."title" AS t1_r16, "artists"."image_description" AS t1_r17, "artists"."search_index_column" AS t1_r18 FROM "songs" LEFT OUTER JOIN "artists" ON "artists"."id" = "songs"."artist_id" WHERE "songs"."title" = 'a.b.c' ORDER BY custom_field

# ActiveRecord::StatementInvalid: PGError: ERROR: column "custom_field" does not exist
# LINE 1: ...tist_id" WHERE "songs"."title" = 'a.b.c' ORDER BY custom_fie...
# ^
```
Whoops, the select `RANDOM() AS custom_field` has been lost somehow.

Am I making a silly mistake or is this a real problem?