# Records in Array

**URL:** <https://www.ruby-forum.com/t/records-in-array/180191>\
**Category:** Rails\
**Created:** [December 14, 2009, 11:47am UTC](https://www.ruby-forum.com/t/records-in-array/180191 "2009-12-14T11:47:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 14, 2009, 11:47am UTC](https://www.ruby-forum.com/t/records-in-array/180191/1 "2009-12-14T11:47:04Z")

</div>

I have a back end admin system for a music library I’m making. I’ve  
atomised my data into 3 tables: Artists, Albums and Songs. They all have  
appropriate associations.

In ‘app\views\artist\_admin\list.html.erb’ I have this code:

\<%= admin\_table({:columns =\>[:name, :last\_updated],  
:list =\> @artists,  
:pages =\> @artist\_pages  
})%\>

I want to integrate a new column that counts the number of songs that  
are associated with the album.

Apologies if this seems trivial; I would’ve googled for this but I  
didn’t quite no what to search for.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 14, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/records-in-array/180191/2 "2009-12-14T16:01:13Z")

</div>

Pale H. wrote:

> I want to integrate a new column that counts the number of songs that  
> are associated with the album.

I would recommend looking at the :counter\_cache option of belongs\_to.

[http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001887&name=belongs\_to](http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001887&name=belongs_to)

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 14, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/records-in-array/180191/3 "2009-12-14T16:06:34Z")

</div>

Robert W. wrote:

> Pale H. wrote:
> 
> > I want to integrate a new column that counts the number of songs that  
> > are associated with the album.
> 
> I would recommend looking at the :counter\_cache option of belongs\_to.
> 
> [http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001887&name=belongs\_to](http://www.railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001887&name=belongs_to)

However, before you do this, consider whether you actually need a  
counter\_cache. SQL’s count function (and its ActiveRecord interface in  
the Calculations module) is really the right way to do this, and you  
should get familiar with it first. IMHO, counter\_cache is simply a  
performance hack on top of that.

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 14, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/records-in-array/180191/4 "2009-12-14T16:29:46Z")

</div>

I can get Album.find(1).songs.size to work in the script console and  
return a valid integer but obviously I need it to be dynamic and cannot  
use Album.find(params[:id]).songs.size as the ID is not defined at this  
point.

Anyway, I’ll have a look into :counter\_cache regardless.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 14, 2009, 7:15pm UTC](https://www.ruby-forum.com/t/records-in-array/180191/5 "2009-12-14T19:15:18Z")

</div>

Marnen Laibow-Koser wrote:

> However, before you do this, consider whether you actually need a  
> counter\_cache. SQL’s count function (and its ActiveRecord interface in  
> the Calculations module) is really the right way to do this, and you  
> should get familiar with it first. IMHO, counter\_cache is simply a  
> performance hack on top of that.

Pale H. wrote:

> I want to integrate a new column that counts the number of songs that  
> are associated with the album.

You’re absolutely right. I should have mentioned that. I was being too  
literal in answering this part of the OP. I agree with you that  
counter\_cache could certainly be considered pre-mature optimization.

By the way. Does Rails override the “size” method on an association by  
using a SQL count? Or, must one use the “count” method to get that  
behavior? Album.find(1).songs.size could be really bad if it does not.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 14, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/records-in-array/180191/6 "2009-12-14T19:56:07Z")

</div>

Robert W. wrote:

> Robert W. wrote:  
> Ugh! Sorry for the bad posting order in the previous reply. I intended  
> the response be directed to Marnen.

Then use quoting in your replies to make it clear. Remember that this  
is primarily a mailing list, even though many of us use the Web forum  
interface.

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [December 14, 2009, 7:17pm UTC](https://www.ruby-forum.com/t/records-in-array/180191/7 "2009-12-14T19:17:02Z")

</div>

Robert W. wrote:  
Ugh! Sorry for the bad posting order in the previous reply. I intended  
the response be directed to Marnen.
