Records in Array

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.

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

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

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
[email protected]

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.

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.

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
[email protected]

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