Skip to content

Instantly share code, notes, and snippets.

@uberllama
Created November 16, 2013 21:46
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 uberllama/7505774 to your computer and use it in GitHub Desktop.
Save uberllama/7505774 to your computer and use it in GitHub Desktop.
# app/models/comment.rb
class Comment < ActiveRecord::Base
include TrackableObserver
belongs_to :user
has_many :activities, as: :trackable
end
# app/models/like.rb
class Like < ActiveRecord::Base
include TrackableObserver
belongs_to :user
has_many :activities, as: :trackable
end
# app/models/activity.rb
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :trackable, polymorphic: true
end
# /app/models/concerns/trackable_observer.rb
module TrackableObserver
extend ActiveSupport::Concern
included do
after_create :create_activity
end
private
def create_activity
activities.create(user_id: user_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment