Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed index view and changed float datatypes for heights

  • Loading branch information...
commit 3c3c4df5c13f87b123820e28e38beb43393c977a 1 parent 7151c84
@felixtsai authored
View
4 app/controllers/guesses_controller.rb
@@ -9,7 +9,9 @@ def new
end
def create
- @guess = Guess.create(params[:guess])
+ @guess = Guess.new(params[:guess])
+ @guess.height = @guess.height/12.0
+ @guess.save
redirect_to @guess
end
View
1  app/controllers/people_controller.rb
@@ -41,6 +41,7 @@ def edit
# POST /people.json
def create
@person = Person.new(params[:person])
+ @person.height = @person.height/12.0
respond_to do |format|
if @person.save
View
1  app/models/guess.rb
@@ -1,6 +1,5 @@
class Guess < ActiveRecord::Base
attr_accessible :height, :weight, :name
- validates :height, :numericality => { :only_integer => true }
validates :weight, :numericality => { :only_integer => true }
end
View
1  app/models/person.rb
@@ -1,7 +1,6 @@
class Person < ActiveRecord::Base
attr_accessible :height, :name, :sex, :weight
- validates :height, :numericality => { :only_integer => true }
validates :weight, :numericality => { :only_integer => true }
after_create :population_add
before_update :population_before_update
View
27 app/models/population_state.rb
@@ -7,7 +7,6 @@ def pop_data
end
def mean(gender, attribute)
- attribute == :height ? pop_data[gender][attribute]/pop_data[gender][:total].to_f/12.0 :
pop_data[gender][attribute]/pop_data[gender][:total].to_f
end
@@ -17,28 +16,16 @@ def overall_prob(gender)
def variance(gender, attribute)
attribute_collection = []
- if attribute == :height
- Person.all.each do |person|
- if person.sex == gender.to_s
- attribute_collection << person.send(attribute)/12.0
- end #end if
- end #end each
- else
- Person.all.each do |person|
- if person.sex == gender.to_s
- attribute_collection << person.send(attribute)
- end #end if
- end #end each
- end
+ Person.all.each do |person|
+ if person.sex == gender.to_s
+ attribute_collection << person.send(attribute)
+ end #end if
+ end #end each
(1/(pop_data[gender][:total] - 1).to_f)* attribute_collection.inject(0) {|sum, attrib| sum + (attrib - mean(gender, attribute))**2}
end #end variance
def attr_prob(gender, attribute, guess_attribute)
- if attribute == :height
- (1/Math.sqrt(2*Math::PI*variance(gender, attribute))) * Math.exp((-(guess_attribute/12.0 - mean(gender, attribute))**2)/(2*variance(gender, attribute)))
- else
- (1/Math.sqrt(2*Math::PI*variance(gender, attribute))) * Math.exp((-(guess_attribute - mean(gender, attribute))**2)/(2*variance(gender, attribute)))
- end
+ (1/Math.sqrt(2*Math::PI*variance(gender, attribute))) * Math.exp((-(guess_attribute - mean(gender, attribute))**2)/(2*variance(gender, attribute)))
end
@@ -59,6 +46,4 @@ def guess(height, weight)
end
end
-
-
end
View
2  app/views/people/index.html.haml
@@ -11,7 +11,7 @@
- @people.each do |person|
%tr
%td= person.name
- %td= person.height
+ %td= person.height.round(2)
%td= person.weight
%td= person.sex
%td
View
13 db/migrate/20121008165654_change_data_type_for_height.rb
@@ -0,0 +1,13 @@
+class ChangeDataTypeForHeight < ActiveRecord::Migration
+ def up
+ change_table :people do |t|
+ t.change :height, :float
+ end
+ end
+
+ def down
+ change_table :people do |t|
+ t.change :height, :integer
+ end
+ end
+end
View
15 db/migrate/20121008184824_change_data_type_for_total_height.rb
@@ -0,0 +1,15 @@
+class ChangeDataTypeForTotalHeight < ActiveRecord::Migration
+ def up
+ change_table :population_states do |t|
+ t.change :total_male_height, :float
+ t.change :total_female_height, :float
+ end
+ end
+
+ def down
+ change_table :population_states do |t|
+ t.change :total_male_height, :float
+ t.change :total_female_height, :float
+ end
+ end
+end
View
13 db/migrate/20121008202457_change_data_type_for_guess_height.rb
@@ -0,0 +1,13 @@
+class ChangeDataTypeForGuessHeight < ActiveRecord::Migration
+ def up
+ change_table :guesses do |t|
+ t.change :height, :float
+ end
+ end
+
+ def down
+ change_table :guesses do |t|
+ t.change :height, :integer
+ end
+ end
+end
View
14 db/schema.rb
@@ -11,20 +11,20 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121005230040) do
+ActiveRecord::Schema.define(:version => 20121008202457) do
create_table "guesses", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name"
- t.integer "height"
+ t.float "height"
t.integer "weight"
t.string "sex"
end
create_table "people", :force => true do |t|
t.string "name"
- t.integer "height"
+ t.float "height"
t.integer "weight"
t.string "sex"
t.datetime "created_at", :null => false
@@ -34,12 +34,12 @@
create_table "population_states", :force => true do |t|
t.integer "total_females", :default => 0
t.integer "total_males", :default => 0
- t.integer "total_female_height", :limit => 255, :default => 0
- t.integer "total_male_height", :default => 0
+ t.float "total_female_height", :limit => 255, :default => 0.0
+ t.float "total_male_height", :default => 0.0
t.integer "total_female_weight", :default => 0
t.integer "total_male_weight", :default => 0
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.