Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

This is fun! #11

Merged
merged 1 commit into from

2 participants

@davekaro

New features to review with new pull requests every day?!

Anyways, the key here is I wanted to use ActiveRecord::Relation to keep the duplication down. When you do something like Athlete.where(:affiliate => 'southie') - Rails will just create an ActiveRecord::Relation object, and not actually hit the database. Then, you can use this object to chain on more conditions, all with never hitting the database. The database will finally be queried once you start to iterate over the results, which usually isn't until the view.

@mikepmunroe mikepmunroe merged commit fd0672b into from
@mikepmunroe
Owner
@davekaro
@mikepmunroe
Owner
@davekaro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 21, 2013
  1. a little less duplication for queries.

    Dave Kroondyk authored
This page is out of date. Refresh to see the latest.
View
16 app/controllers/southie_controller.rb
@@ -1,12 +1,14 @@
class SouthieController < ApplicationController
def index
- @athletes = Athlete.where(:affiliate => 'southie').order("total asc")
- @individual_men = Athlete.where(:affiliate => 'southie').where('age < 40').where('gender == "Male"').order('total asc')
- @masters_40_44_men = Athlete.where(:affiliate => 'southie').where('age > 39 and age < 45').where('gender == "Male"').order('total asc')
- @masters_50_54_men = Athlete.where(:affiliate => 'southie').where('age > 49 and age < 55').where('gender == "Male"').order('total asc')
- @masters_55_59_men = Athlete.where(:affiliate => 'southie').where('age > 54 and age < 60').where('gender == "Male"').order('total asc')
- @masters_60_men = Athlete.where(:affiliate => 'southie').where('age > 60').where('gender == "Male"').order('total asc')
- @individual_women = Athlete.where(:affiliate => 'southie').where('age < 40').where('gender == "Female"').order('total asc')
+ athletes = Athlete.where(:affiliate => 'southie').order('total asc')
+ male_athletes = athletes.where("gender == 'Male'")
+ female_athletes = athletes.where("gender == 'Female'")
+ @individual_men = male_athletes.where('age < 40')
+ @masters_40_44_men = male_athletes.where('age > 39 and age < 45')
+ @masters_50_54_men = male_athletes.where('age > 49 and age < 55')
+ @masters_55_59_men = male_athletes.where('age > 54 and age < 60')
+ @masters_60_men = male_athletes.where('age > 60')
+ @individual_women = female_athletes.where('age < 40')
end
end
View
20 app/controllers/wicked_controller.rb
@@ -1,14 +1,16 @@
class WickedController < ApplicationController
def index
- @athletes = Athlete.where(:affiliate => 'wicked').order("total asc")
- @individual_men = Athlete.where(:affiliate => 'wicked').where('age < 40').where('gender == "Male"').order('total asc')
- @masters_40_44_men = Athlete.where(:affiliate => 'wicked').where('age > 39 and age < 45').where('gender == "Male"').order('total asc')
- @masters_45_49_men = Athlete.where(:affiliate => 'wicked').where('age > 44 and age < 50').where('gender == "Male"').order('total asc')
- @masters_50_54_men = Athlete.where(:affiliate => 'wicked').where('age > 49 and age < 55').where('gender == "Male"').order('total asc')
- @individual_women = Athlete.where(:affiliate => 'wicked').where('age < 40').where('gender == "Female"').order('total asc')
- @masters_40_44_women = Athlete.where(:affiliate => 'wicked').where('age > 39 and age < 45').where('gender == "Female"').order('total asc')
- @masters_45_49_women = Athlete.where(:affiliate => 'wicked').where('age > 44 and age < 50').where('gender == "Female"').order('total asc')
- @masters_55_59_women = Athlete.where(:affiliate => 'wicked').where('age > 54 and age < 60').where('gender == "Female"').order('total asc')
+ athletes = Athlete.where(:affiliate => 'wicked').order('total asc')
+ male_athletes = athletes.where("gender == 'Male'")
+ female_athletes = athletes.where("gender == 'Female'")
+ @individual_men = male_athletes.where('age < 40')
+ @masters_40_44_men = male_athletes.where('age > 39 and age < 45')
+ @masters_45_49_men = male_athletes.where('age > 44 and age < 50')
+ @masters_50_54_men = male_athletes.where('age > 49 and age < 55')
+ @individual_women = female_athletes.where('age < 40')
+ @masters_40_44_women = female_athletes.where('age > 39 and age < 45')
+ @masters_45_49_women = female_athletes.where('age > 44 and age < 50')
+ @masters_55_59_women = female_athletes.where('age > 54 and age < 60')
end
end
Something went wrong with that request. Please try again.