Skip to content

Instantly share code, notes, and snippets.

@dnch
Created April 28, 2009 00:35
Show Gist options
  • Save dnch/102847 to your computer and use it in GitHub Desktop.
Save dnch/102847 to your computer and use it in GitHub Desktop.
# assuming the following columns in `things`
#
# children_counter_cache :integer
# parent_id :integer
#
class Thing < ActiveRecord::Base
has_many :children, :class_name => "Thing", :foreign_key => "parent_id", :dependent => :nullify
belongs_to :parent, :class_name => "Thing", :foreign_key => "parent_id", :counter_cache => 'children_counter_cache'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment