views:

57

answers:

1

I'm trying to find the count of the number of devices that are greater than the amount from a certain data source.

last_data_source = LocalDeviceSummary.find(:first, :order => 'created_at desc')
count = RemoteDeviceSummary.count ["DeviceSummaryIndex > ?", last_data_source.data_source_id]

The last_data_source works fine, I tested that. The error is with the count. Here is the error.

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 '?2896012) AS count_devicesummaryindex_2896012 FROM device_summary' at line 1: SELECT count(DeviceSummaryIndex > ?2896012) AS count_devicesummaryindex_2896012 FROM device_summary

+1  A: 

When you pass an Array to ActiveRecord::Count you need to specify your input array with the :conditions symbol. This should work for you:

count = RemoteDeviceSummary.count(:conditions => ["DeviceSummaryIndex > ?", last_data_source.data_source_id])
Cody Caughlan
This is coming back as zero everytime?
Ryan
Well I dont know the structure of your tables, so I cannot help you determine *why* you are getting zero back every time. I know the first time around you were not calling the count method correctly, so this is the correct invocation pattern for it. As for why it returns zero every time, it depends on your data set.
Cody Caughlan
Its giving me 0 every time too.
inKit