Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

has_many :through + scope + empty? fails in 2.3.2 (but not 2.3.0)

#2310

A Post has_many Tags through Taggings.

Tag has this named scope:


named_scope :by_name, { :order => 'name ASC' }

In Rails 2.2.2 and 2.3.0, this worked fine:


a_post.tags.by_name.empty?

It generates a query like


SELECT count(*) AS count_all FROM `tags` INNER JOIN `taggings` ON `tags`.id = `taggings`.tag_id WHERE ((`taggings`.post_id = 881129040)) 

In Rails 2.3.2, it breaks:


  SQL (0.0ms)   Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) AS count_tags_all FROM `tags` INNER JOIN `taggings` ON `tags`.id = `taggings`' at line 1: SELECT count(`tags`.*) AS count_tags_all FROM `tags` INNER JOIN `taggings` ON `tags`.id = `taggings`.tag_id WHERE (((`taggings`.post_id = 881129040)) AND ((`taggings`.post_id = 881129040))) 

The (invalid) query generated here is


SELECT count(`tags`.*) AS count_tags_all FROM `tags` INNER JOIN `taggings` ON `tags`.id = `taggings`.tag_id WHERE (((`taggings`.post_id = 881129040)) AND ((`taggings`.post_id = 881129040)))

I'm sorry I don't have more details or a failing spec yet. Figured I'd report the issue for now, and possibly dig into later.

Reported by Henrik Nyh · March 22nd, 2009 @ 01:28 PM

State: duplicate
Milestone: 2.x
Assigned to: Ryan Bigg Ryan Bigg
Importance: none

Activity

  1. Henrik Nyh
    Henrik Nyh

    Probably related:

    
    p = Post.last
    t = Tag.last
    p.tags << t
    

    gives

    
    ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) AS count_posts_all FROM `posts` INNER JOIN `taggings` ON `posts`.id = `taggin' at line 1: SELECT count(`posts`.*) AS count_posts_all FROM `posts` INNER JOIN `taggings` ON `posts`.id = `taggings`.post_id WHERE (((posts.published_at IS NOT NULL) AND ((`taggings`.tag_id = 834120776))) AND ((`taggings`.tag_id = 834120776))) 
    

    Where the invalid query is

    
    SELECT count(`posts`.*) AS count_posts_all FROM `posts` INNER JOIN `taggings` ON `posts`.id = `taggings`.post_id WHERE (((posts.published_at IS NOT NULL) AND ((`taggings`.tag_id = 834120776))) AND ((`taggings`.tag_id = 834120776))) 
    

    March 22nd, 2009 @ 02:02 PM

  2. Steven Soroka
    Steven Soroka

    I'm having the exact same problem. :/

    March 24th, 2009 @ 02:03 AM

  3. Steven Soroka
    Steven Soroka
    • State changed from new to resolved

    This bug was introduced by commit 6543426... Ensure calculations respect scoped :select [#1334 state:resolved]

    March 26th, 2009 @ 04:03 PM

  4. Steven Soroka
    Steven Soroka
    • State changed from resolved to new

    uh. that was not supposed to change the state. wonder if this will change it back: [#2310 state:new]

    March 26th, 2009 @ 04:05 PM

  5. Steven Soroka
    Steven Soroka

    this is the same problem referenced in #2189

    March 26th, 2009 @ 04:12 PM

  6. Steve St. Martin
    Steve St. Martin
    • Assigned user set to Ryan Bigg

    duplicates #2189, can be marked as duplicate

    April 15th, 2010 @ 11:53 PM

  7. Ryan Bigg
    Ryan Bigg
    • State changed from new to duplicate

    April 16th, 2010 @ 01:24 AM