Is it possible to make a query like this with rails?
SELECT items.*, (select count(*) from ads where item_id = items.id) as adscount WHERE 1
And then access this field like so?
@item.adscount
For example for each item there is a hundred ads or so.
And in items index view I need to show how many ads each item has.
For now I only found out how to make a unique query for every item, for example:
select count(*) from ads where item_id = 1
select count(*) from ads where item_id = 2
select count(*) from ads where item_id = 3
etc
Edit:
Made a counter cache column.
That gave me huge performance improvement.
Use Rails
Counter Cachehttp://railscasts.com/episodes/23-counter-cache-columnThen you will be able to use it like
@item.ads.countand this will not produce any database queries.And in addition (if you really want to use it your way) you can create a model method