Skip to content

Instantly share code, notes, and snippets.

@abrambailey
Created August 12, 2012 06:49
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 abrambailey/3330275 to your computer and use it in GitHub Desktop.
Save abrambailey/3330275 to your computer and use it in GitHub Desktop.
blah
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
belongs_to :user
attr_accessible :body, :user
validates_length_of :body, :minimum => 1
validates_presence_of :user
end
#So we have this comment class, that we can also think of as favorite
We can write a neat mixin that makes it so we can make anything commentable
module Commentable
extend ActiveSupport::Concern
included do
attr_accessible :comments
has_many :comments, :as => :commentable, :autosave => true
accepts_nested_attributes_for :comments
def add_comment(author, body)
self.comments << Comment.create!(:admin => author, :body => body)
save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment