Skip to content
Browse files

schools stats for our own benefit, added registrations counter cache

  • Loading branch information...
1 parent 5f1ba5f commit ef90454c229aa8177bed78e1fab48a2621c8a3ef @croaky croaky committed Feb 3, 2011
View
5 app/controllers/schools_controller.rb
@@ -0,0 +1,5 @@
+class SchoolsController < ApplicationController
+ def index
+ send_data School.stats
+ end
+end
View
2 app/models/registration.rb
@@ -3,7 +3,7 @@ class Registration < ActiveRecord::Base
validates_uniqueness_of :twitter, :if => :twitter_present?
validates_inclusion_of :role, :in => %w(Designer Developer)
- belongs_to :school
+ belongs_to :school, :counter_cache => true
attr_writer :school_name
before_create :find_or_create_school
View
10 app/models/school.rb
@@ -5,6 +5,14 @@ class School < ActiveRecord::Base
has_many :registrations
def self.names
- order('name asc').map(&:name)
+ alphabetical.map(&:name)
+ end
+
+ def self.alphabetical
+ order('name asc')
+ end
+
+ def self.stats
+ order('registrations_count desc').to_csv(:only => [:name, :registrations_count])
end
end
View
1 config/routes.rb
@@ -7,6 +7,7 @@
resources :developers, :only => [:index]
resources :startups, :only => [:index, :show]
resources :other_events, :only => [:index]
+ resources :schools, :only => [:index]
match 'pages/what' => 'talks#index'
end
View
16 db/migrate/20110203175114_add_registrations_counter_cache_column.rb
@@ -0,0 +1,16 @@
+class AddRegistrationsCounterCacheColumn < ActiveRecord::Migration
+ def self.up
+ add_column :schools, :registrations_count, :integer, :default => 0
+
+ schools = select_all "select * from schools"
+ schools.each do |school|
+ registrations_count = select_value "select count(*) from registrations where (registrations.school_id = #{school['id']})"
+
+ update "update schools set registrations_count = #{registrations_count} where schools.id = #{school['id']}"
+ end
+ end
+
+ def self.down
+ remove_column :schools, :registrations_count
+ end
+end

0 comments on commit ef90454

Please sign in to comment.
Something went wrong with that request. Please try again.