-
-
Save radar/742770 to your computer and use it in GitHub Desktop.
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 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