You would probably want to make a counter cache for tasks on the
project (you can find out more here:
),
and additional when retrieving tasks from the database, when you
know you will need their project data, use eager loading (more here:
).
Michał.
+1 what Michał said about eager-loading. One additional tricky thing: prefer size over length for associations and relations. In plain Ruby, size, length and count are more or less identical - but for ActiveRecord collections they have slightly different meanings:
length is the most straightforward: it’s ALWAYS the number of records in the collection. If the collection isn’t currently loaded, calling length on it will trigger a SQL query to load all the records.
count is the opposite: it ALWAYS runs a SQL query. It doesn’t load records, it uses SQL’s COUNT() function. It can also return things that aren’t numbers; doing Task.group(:project_id).count will give you back a hash with project_ids as keys and the number of matching tasks as values.
size is the middle: if the collection is loaded, it works like length. Otherwise it works like count…