Rails bug?
Friday, April 18th, 2008I discovered what looks like a bug with has_many :finder_sql. The docs claim that if you supply :finder_sql and not :counter_sql, it makes the counter by replacing “SELECT * FROM …” with “SELECT COUNT(*) FROM …”.
This does not appear to work if your :finder_sql has a subquery. To make up a fake example:
class User
has_many :comments_not_mine,
:class_name => 'Comment',
:finder_sql => 'SELECT * FROM comments WHERE user_id != "#{id}" AND EXISTS (SELECT * FROM users)'
end
(The EXISTS part is silly, and just there to show a subquery.)
Now I can say user.comments_not_mine and it works fine, but user.comments_not_mine.count gives an error about the SQL "SELECT COUNT(*) FROM users)". It appears to be using a greedy regexp by mistake. (user.comments_not_mine.to_a.size works fine. It’s just the #count method it adds is trying to be clever, and failing.)
For the life of me, I can’t find it, so: dear lazyweb, please find the offending regexp, so I can write a patch.
