-
-
Save abrambailey/3330275 to your computer and use it in GitHub Desktop.
blah
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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