Skip to content

Instantly share code, notes, and snippets.

@arbarlow
Created May 25, 2011 15:59
Show Gist options
  • Save arbarlow/991240 to your computer and use it in GitHub Desktop.
Save arbarlow/991240 to your computer and use it in GitHub Desktop.
class Release < ActiveRecord::Base
has_many :songs
end
# Tell Rails, update a "songs_count" field on the parent record after creation/deletion
class Song < ActiveRecord::Base
belongs_to :release, :counter_cache => true
end
r = Release.first
r.songs_count # No SQL is called for the count. Just uses the attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment