Hi all,

I'm not sure if this is a bug or a feature, but I had a question about
how counting on association works.

I have a Resort model and an Update model, and a Resort has many
Updates. I also have method in Resort that returns a count of how many
updates in the last day, like so:

def updates_in_last_day
  updates.count(:date.gte => (Time.now - (3600*24)))
end

Which yields a query like this:

SELECT COUNT(*) FROM `updates` WHERE (`resort_id` = 1) AND (`id` IN
(8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
94, 95, 96, 97, 98)) AND (`date` >= '2008-12-13 13:17:27')

Since I already have a foreign key lookup on the association, it seems
like I wouldn't need the "id in..." clause of this query.

I figured out that this only happens after calling .updates on a
Resort instance (e.g. iterating through all the updates). If I don't
iterate through the updates first, I get a query that seems more
reasonable:

SELECT COUNT(*) FROM `updates` WHERE (`resort_id` = 2) AND (`date` >=
'2008-12-13 13:39:03')

What concerns me is that the list of IDs will grow infinitely as more
data is added, which doesn't seem good for performance.

Thanks for any insight,
-Matt
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to