Skip to content

Instantly share code, notes, and snippets.

@Chryus
Last active February 17, 2017 17:18
Show Gist options
  • Save Chryus/6baacefdc436e92591847c22fe8ffd37 to your computer and use it in GitHub Desktop.
Save Chryus/6baacefdc436e92591847c22fe8ffd37 to your computer and use it in GitHub Desktop.
# graffiti.rb
class Graffiti < ActiveRecord::Base
belongs_to :artist, class_name: "User"
has_many :uploads
has_many :comments
has_many :upvotes, as: :upvotable
end
# upload.rb
class Upload < ApplicationRecord
belongs_to :user
belongs_to :graffiti
has_many :upvotes, as: :upvotable
end
class Comment < ApplicationRecord
belongs_to :user
belongs_to :graffiti
has_many :upvotes, as: :upvotable
end
# upvote.rb
class Upvote < ApplicationRecord
belongs_to :user
belongs_to :upvotable, polymorphic: true
end
class User < ApplicationRecord
has_many :upvotes
has_many :uploads, through: :upvotes
has_many :graffiti, through: :upvotes
has_many :works, class_name: "Graffiti", foreign_key: "user_id"
has_many :comments
end
# schema
create_table :upvotes do |t|
t.integer :user_id
t.integer :upvotable_id
t.string :upvotable_type
t.timestamps
end
add_index :upvotes, [:upvotable_type, :upvotable_id]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment