Skip to content

Instantly share code, notes, and snippets.

@radar
Forked from Brook/gist:742767
Created December 15, 2010 23:25
Show Gist options
  • Select an option

  • Save radar/742770 to your computer and use it in GitHub Desktop.

Select an option

Save radar/742770 to your computer and use it in GitHub Desktop.
class TopicAssociation < ActiveRecord::Base
belongs_to :user
belongs_to :topic
after_create :calculate_popularity
validates_uniqueness_of :user_id, :scope => :topic_id
def calculate_popularity
self.popularity = TopicAssociation.count(:all, :conditions=>{:topic_id => self.topic_id})
TopicAssociation.update_all( "popularity = #{self.popularity}","topic_id=#{self.topic_id}" )
end
end
class Topic < ActiveRecord::Base
has_many :comments
has_many :topic_associations
has_many :users, :through => :topic_associations
end
class User < ActiveRecord::Base
has_many :topic_associations
has_many :topics, :through => :topic_associations
has_many :topic_user_owns, :foreign_key => :user_id
has_many :comments
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["user_info"]["nickname"]
end
end
end
class TopicsController < ApplicationController
def index
@topics = Topic.all(:include => :topic_associations, :order => 'popularity DESC')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment