Skip to content

Instantly share code, notes, and snippets.

@jnwheeler44
Created November 3, 2012 06:25
Show Gist options
  • Select an option

  • Save jnwheeler44/4006272 to your computer and use it in GitHub Desktop.

Select an option

Save jnwheeler44/4006272 to your computer and use it in GitHub Desktop.
class Chair < ActiveRecord::Base
belongs_to :person
belongs_to :location
has_one :result
has_many :reports, :through=>:result
def self.get_home_page_data(location, user)
# Looking at other spots in the code, I see that the individual fields
# aren't relevant, and the extra selected fields aren't referenced.
# This whole block will be removed
sql = 'SELECT DISTINCT c.id, c.received_on, c.info, c.number, c.condition_id,
p.first_name, p.last_name, p.middle_initial, '
sql += ' rp.active, rp.viewed, rp.printed, rs.diagnosis_id '
# It seems that joins weren't an obvious choice here, for
# some reason. I can't think of a good one, so we'll change
# that. We're also not going to do a find_by_sql, anymore.
# OLD:
sql += "FROM chairs c, reports rp, results rs, people p "
sql += "WHERE rs.chairs_id = chairs.id AND "
sql += "rp.result_id = rs.id AND "
sql += "c.person_id = p.id AND "
sql += "c.location_id = #{location}"
# NEW:
Chair.joins(:person, :reports, :location)
# Permissions are being checked here. We don't have to
# resort to manually appending all of these OR statements
# and then chopping off that last 'OR ' (whole other issue)
# OLD:
search_user = User.find(user)
for permission in search_user.chair_permissions
sql += "person_id ='#{permission.owner_id}' OR "
end
# NEW:
allowed_user_ids = user.chair_permissions.collect(&:owner_id)
Chair.joins(:person, :reports, :location).where(:person_id => allowed_user_ids)
# Now we just add these to our query chain.
# OLD:
sql += "rp.active = 1 AND"
sql += "rp.viewed = 0 AND "
# NEW:
allowed_user_ids = user.chair_permissions.collect(&:owner_id)
Chair.joins(:person, :reports, :location)
.where(:person_id => allowed_user_ids)
.where(:reports => { :active => 1, :viewed => 1 })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment