#count, :match_all => true causing ActiveRecord::StatementInvalid on Postgres #242

Closed
ahawkins opened this Issue Mar 26, 2012 · 3 comments

Projects

None yet

2 participants

@ahawkins

For some reason :match_all => true is causing postgres to throw a syntax error related to "AS". Here is my debugging session from the tests.

(rdb:1) Activity.tagged_with(['call_list', 'created']).count
1

(rdb:1) Activity.tagged_with(['call_list', 'created'], :match_all => true).count
ActiveRecord::StatementInvalid Exception: PG::Error: ERROR:  syntax error at or near "AS"
LINE 1: ... COUNT(activities_taggings_group.taggable_id) = 2 AS activit...
                                                             ^
: SELECT COUNT(*) AS count_all, activities.id, activities.reference_id, activities.reference_type, activities.account_id, activities.owner_id, activities.owner_type, activities.created_at, activities.updated_at, activities.timestamp 
HAVING COUNT(activities_taggings_group.taggable_id) = 2 AS activities_id_activities_reference_id_activities_reference_type FROM "activities" JOIN taggings activities_taggings_a00c683  ON activities_taggings_a00c683.taggable_id = activities.id AND activities_taggings_a00c683.taggable_type = 'Activity' AND activities_taggings_a00c683.tag_id = 2 JOIN taggings activities_taggings_0980a52  ON activities_taggings_0980a52.taggable_id = activities.id AND activities_taggings_0980a52.taggable_type = 'Activity' AND activities_taggings_0980a52.tag_id = 3 LEFT OUTER JOIN taggings activities_taggings_group  ON activities_taggings_group.taggable_id = activities.id AND activities_taggings_group.taggable_type = 'Activity' GROUP BY activities.id, activities.reference_id, activities.reference_type, activities.account_id, activities.owner_id, activities.owner_type, activities.created_at, activities.updated_at, activities.timestamp HAVING COUNT(activities_taggings_group.taggable_id) = 2

Formatted query so it's easier to read:

SELECT COUNT(*) 
  AS count_all, activities.id, activities.reference_id, 
    activities.reference_type, activities.account_id, 
    activities.owner_id, activities.owner_type, activities.created_at, 
    activities.updated_at, activities.timestamp 
HAVING COUNT(activities_taggings_group.taggable_id) = 2 AS activities_id_activities_reference_id_activities_reference_type 
FROM "activities" 
JOIN taggings activities_taggings_a00c683 
  ON activities_taggings_a00c683.taggable_id = activities.id
    AND activities_taggings_a00c683.taggable_type = 'Activity' 
    AND activities_taggings_a00c683.tag_id = 2 
JOIN taggings activities_taggings_0980a52 
  ON activities_taggings_0980a52.taggable_id = activities.id 
  AND activities_taggings_0980a52.taggable_type = 'Activity' 
  AND activities_taggings_0980a52.tag_id = 3 
LEFT OUTER JOIN taggings activities_taggings_group
  ON activities_taggings_group.taggable_id = activities.id
  AND activities_taggings_group.taggable_type = 'Activity' 
GROUP BY 
  activities.id, activities.reference_id, activities.reference_type, 
  activities.account_id, activities.owner_id, activities.owner_type, 
  activities.created_at, activities.updated_at, activities.timestamp 
HAVING COUNT(activities_taggings_group.taggable_id) = 2
@ahawkins

Seems the HAVING COUNT bit is being generated twice. Why, I don't know.

@ahawkins

Apparently, if :group is passed to the underlying scoped call and #count is called, the HAVING claus is generated twice.

@ahawkins

This is a Rails issue. Not this gem's problem. I believe this gem is calling scoped according to the public API documentation. In that case, acts-as-taggable-on is delegating to ActiveRecord.

I've opened an issue here: rails/rails#5588.

@artemk artemk pushed a commit that closed this issue Jul 9, 2012
twinturbo Close #242 01ceb00
@artemk artemk closed this in 01ceb00 Jul 9, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment