# Mysql error with named\_scope and any?

**URL:** <https://www.ruby-forum.com/t/mysql-error-with-named-scope-and-any/172027>\
**Category:** Rails\
**Created:** [July 13, 2009, 3:52pm UTC](https://www.ruby-forum.com/t/mysql-error-with-named-scope-and-any/172027 "2009-07-13T15:52:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [July 13, 2009, 3:52pm UTC](https://www.ruby-forum.com/t/mysql-error-with-named-scope-and-any/172027/1 "2009-07-13T15:52:03Z")

</div>

Hi - need some help to pin this one.

Am upgrading a project to 2.3.2. Have changed has\_finder to  
named\_scope. But I am getting an invalid mysql syntax error with a  
finder that used to work

I have:

Batch  
has\_many :orders  
has\_many :line\_items, :through=\>:orders

Order  
has\_many :line\_items

LineItem  
named\_scope :unmatched, :conditions =\> {‘product\_id’=\>0}

@batch.line\_items.unmatched.any?  
gives me: 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\_line\_items\_all FROM `line_items` INNER  
JOIN `orders` ON `line_items`’ at line 1: SELECT count(`line_items`._)  
AS count\_line\_items\_all FROM `line_items` INNER JOIN `orders` ON  
`line_items`.order\_id = `orders`.id WHERE (((`line_items`.`product_id`  
= 0) AND ((`orders`.batch\_id = 1479))) AND ((`orders`.batch\_id =  
1479)))

If I run the query, I can make it work with count(\*) or count  
(line\_items.id) - is this a mysql problem?  
Also note the batch id condition is included twice.

If I do  
@batch.line\_items.any?  
I get a valid query:  
SELECT count(\*) AS count\_all FROM `line_items` INNER JOIN `orders` ON  
`line_items`.order\_id = `orders`.id WHERE ((`orders`.batch\_id = 1479))

So am I doing something bad with my named\_scope??

Thanks for any help  
Tonypm
