From 819ff49c631c9e6876404b01a559ee1006be2ca5 Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Thu, 20 Dec 2018 09:06:47 -0500 Subject: [PATCH] adding SchoolQuestion --- Gemfile | 2 +- Gemfile.lock | 3 + app/models/school.rb | 4 +- app/models/school_category.rb | 2 + app/models/school_question.rb | 13 ++++ .../20181218154720_add_school_questions.rb | 18 ++++++ db/schema.rb | 22 ++++++- lib/tasks/data.rake | 59 +++++++++++++++++++ 8 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 app/models/school_question.rb create mode 100644 db/migrate/20181218154720_add_school_questions.rb diff --git a/Gemfile b/Gemfile index 73bce339..3be4ddf5 100644 --- a/Gemfile +++ b/Gemfile @@ -52,7 +52,7 @@ gem 'omniauth' gem 'twilio-ruby', '~> 4.11.1' - +gem 'activerecord-import' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index 26e0bfc4..6977b61f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -33,6 +33,8 @@ GEM activemodel (= 5.0.7) activesupport (= 5.0.7) arel (~> 7.0) + activerecord-import (0.27.0) + activerecord (>= 3.2) activesupport (5.0.7) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) @@ -250,6 +252,7 @@ PLATFORMS ruby DEPENDENCIES + activerecord-import bootstrap (~> 4.0.0.alpha6) byebug capybara diff --git a/app/models/school.rb b/app/models/school.rb index ce005b99..24177801 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -14,9 +14,9 @@ class School < ApplicationRecord friendly_id :name, :use => [:slugged] def available_responders_for(question) - if question.target_group == "for_students" + if question.for_students? return student_count || 1 - elsif question.target_group == "for_teachers" + elsif question.for_teachers? return teacher_count || 1 else return 1 diff --git a/app/models/school_category.rb b/app/models/school_category.rb index 42469bee..359b61ab 100644 --- a/app/models/school_category.rb +++ b/app/models/school_category.rb @@ -5,6 +5,8 @@ class SchoolCategory < ApplicationRecord belongs_to :school belongs_to :category + has_many :school_questions + validates_associated :school validates_associated :category diff --git a/app/models/school_question.rb b/app/models/school_question.rb new file mode 100644 index 00000000..b722a8f9 --- /dev/null +++ b/app/models/school_question.rb @@ -0,0 +1,13 @@ +class SchoolQuestion < ApplicationRecord + + belongs_to :school + belongs_to :question + belongs_to :school_category + + validates_associated :school + validates_associated :question + validates_associated :school_category + + scope :for, -> (school, question) { where(school_id: school.id, question_id: question.id) } + +end diff --git a/db/migrate/20181218154720_add_school_questions.rb b/db/migrate/20181218154720_add_school_questions.rb new file mode 100644 index 00000000..6b029680 --- /dev/null +++ b/db/migrate/20181218154720_add_school_questions.rb @@ -0,0 +1,18 @@ +class AddSchoolQuestions < ActiveRecord::Migration[5.0] + def change + create_table :school_questions do |t| + t.integer :school_id + t.integer :question_id + t.integer :school_category_id + t.integer :attempt_count + t.integer :response_count + t.float :response_rate + t.string :year + + t.timestamps + end + + add_column :school_categories, :valid_child_count, :integer + add_column :school_categories, :response_rate, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 74c627d8..d142f07e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180711164051) do +ActiveRecord::Schema.define(version: 20181218154720) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -152,17 +152,33 @@ ActiveRecord::Schema.define(version: 20180711164051) do t.float "nonlikert" t.float "zscore" t.string "year" + t.integer "valid_child_count" + t.integer "response_rate" t.index ["category_id"], name: "index_school_categories_on_category_id", using: :btree t.index ["school_id"], name: "index_school_categories_on_school_id", using: :btree end + create_table "school_questions", force: :cascade do |t| + t.integer "school_id" + t.integer "question_id" + t.integer "school_category_id" + t.integer "attempt_count" + t.integer "response_count" + t.float "response_rate" + t.string "year" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "schools", force: :cascade do |t| t.string "name" t.integer "district_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.text "description" t.string "slug" + t.integer "student_count" + t.integer "teacher_count" t.index ["slug"], name: "index_schools_on_slug", unique: true, using: :btree end diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index b12b0fdf..b5f4fa06 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -501,3 +501,62 @@ end # puts "" # puts "MISSING SCHOOLS: #{missing_schools.length}" # missing_schools.each { |s| puts(s) } +# +# +# Category.joins(:questions).uniq.all.each do |category| +# category.school_categories.includes(school: [:district]).find_in_batches(batch_size: 100) do |group| +# group.each do |school_category| +# school_questions = [] +# +# category.questions.created_in(school_category.year).each do |question| +# school = school_category.school +# next if school.district.name != "Boston" +# attempt_count = Attempt. +# created_in(school_category.year). +# for_question(question). +# for_school(school).count +# +# available_responders = school.available_responders_for(question) +# school_questions << school_category.school_questions.new( +# school: school, +# question: question, +# school_category: school_category, +# year: school_category.year, +# attempt_count: available_responders, +# response_count: attempt_count, +# response_rate: attempt_count.to_f / available_responders.to_f +# ) +# end +# +# SchoolQuestion.import school_questions +# valid_questions = school_questions.select { |sc| sc.response_rate > 0.3 } +# school_category.update( +# valid_child_count: valid_questions.length +# ) +# end +# +# end +# end +# +# loop do +# categories = Category.joins(:school_categories) +# .merge(SchoolCategory.where("valid_child_count is not null")) +# .uniq +# break if categories.count == 0 +# categories.all.each do |category| +# parent_category = category.parent_category +# +# parent_category.school_categories.find_in_batches(batch_size: 100) do |group| +# group.each do |school_category| +# next if school_category.valid_child_count.present? +# +# school = school_category.school +# children = SchoolCategory.for_parent_category(school, parent_category).in(school_category.year) +# school_category.update( +# valid_child_count: children.where("valid_child_count > 1").count +# ) +# end +# end +# end +# end +# end