ruby user
2014-Sep-03  17:38 UTC
Select with an alias does not show alias column in "having".
The Model Image has many Tags
When i run this query to get Images with tags that are named both bamboo in 
and Green i get an error that says the alias table is not present. What am 
I doing wrong?
Image.joins(:tags).select("images.*, count(tags.id) as 
'tags_count'").where(tags:{name:["bamboo","Green"]}).group("images.id").having("tags_count
= 2")
Error 
*Image Load (0.4ms)*  SELECT images.*, count(tags.id) as tags_count FROM 
"images" INNER JOIN "image_tags" ON
"image_tags"."image_id" = "images"."id"
INNER JOIN "tags" ON "tags"."id" =
"image_tags"."tag_id" WHERE
"tags"."name" IN ('bamboo', 'Green') GROUP
BY images.id HAVING tags_count =
2
PG::UndefinedColumn: ERROR:  column "tags_count" does not exist
LINE 1: ... IN ('bamboo', 'Green') GROUP BY images.id HAVING
tags_count...
                                                             ^
: SELECT images.*, count(tags.id) as tags_count FROM "images" INNER
JOIN
"image_tags" ON "image_tags"."image_id" =
"images"."id" INNER JOIN "tags"
ON "tags"."id" = "image_tags"."tag_id"
WHERE "tags"."name" IN ('bamboo',
'Green') GROUP BY images.id HAVING tags_count = 2
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column 
"tags_count" does not exist
LINE 1: ... IN ('bamboo', 'Green') GROUP BY images.id HAVING
tags_count...
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/c5e93122-3e6a-4e48-907d-79600d819b74%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.