This project is archived and is in readonly mode.

#2310 ✓duplicate
Henrik Nyh

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

Reported by Henrik Nyh | March 22nd, 2009 @ 01:28 PM | in 2.x

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.

Comments and changes to this ticket

  • Henrik Nyh

    Henrik Nyh March 22nd, 2009 @ 02:02 PM

    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))) 
    
  • Steven Soroka

    Steven Soroka March 24th, 2009 @ 02:03 AM

    I'm having the exact same problem. :/

  • Steven Soroka

    Steven Soroka March 26th, 2009 @ 04:03 PM

    • State changed from “new” to “resolved”

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

  • Steven Soroka

    Steven Soroka March 26th, 2009 @ 04:05 PM

    • 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]

  • Steven Soroka

    Steven Soroka March 26th, 2009 @ 04:12 PM

    this is the same problem referenced in #2189

  • Steve St. Martin

    Steve St. Martin April 15th, 2010 @ 11:53 PM

    • Assigned user set to “Ryan Bigg”

    duplicates #2189, can be marked as duplicate

  • Ryan Bigg

    Ryan Bigg April 16th, 2010 @ 01:24 AM

    • State changed from “new” to “duplicate”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages