Hey,
Pretty much a follow up to my previous thread on here but thought it warranted it's own topic.
Background:
I have 3 models:
Code:Collections has_many collections as children belongs_to collection as parent Albums belongs_to collection has_many photos Photos belongs_to album
Basically I want to write an instance method for a collection to return the total number of photos of ALL children albums including all those albums in children collections of the calling collection.
So for example:
Calling photo_count on collection 1 would return 21 and calling it on collection 2 would return 11Code:Collection 1 Collection 2 Album 1 - 5 photos Album 2 - 6 photos Collection 3 Album 3 - 10 photos
I have tried to write it as follows but it's not quite right, I realise I need to use a try method somewhere but i'm just not sure how to finish it off.
Code Ruby:def photo_count count = 0 albums.each do |album| count += album.photo_count end end
Could anyone give me a hand here?
Neil



Reply With Quote




Bookmarks