Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

schools stats for our own benefit, added registrations counter cache

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

0 comments on commit ef90454

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