Skip to content

Instantly share code, notes, and snippets.

@pacoguzman
Created December 12, 2009 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pacoguzman/254989 to your computer and use it in GitHub Desktop.
Save pacoguzman/254989 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
before_create :create_profile
has_one :profile, :dependent => :destroy
protected
def create_profile
self.profile ||= Profile.new
end
end
class Profile < ActiveRecord::Base
after_create :create_root_graffity
belongs_to :user
has_one :root_graffity, :class_name => 'Comment', :as => :commentable, :conditions => "parent_id IS NULL"
def graffities
root_graffity.sons
end
protected
def create_root_graffity
self.root_graffity = Comment.new(:user => self.user)
self.save(false)
end
end
class Comment < ActiveRecord::Base
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
validates_presence_of :body
validates_presence_of :user
# NOTE: install the acts_as_votable plugin if you
# want user to vote on the quality of comments.
#acts_as_voteable
# NOTE: Comments belong to a user
belongs_to :user
belongs_to :commentable, :polymorphic => true
has_many :sons, :class_name => 'Comment',
:foreign_key => "parent_id", :order => "created_at DESC"
has_many :replys, :class_name => "Comment",
:foreign_key => "parent_id", :order => "created_at ASC"
# EXTRA CODE HERE
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment