Skip to content

Instantly share code, notes, and snippets.

@Jragon
Created December 17, 2013 16:59
Show Gist options
  • Select an option

  • Save Jragon/8008348 to your computer and use it in GitHub Desktop.

Select an option

Save Jragon/8008348 to your computer and use it in GitHub Desktop.
class Change < ActiveRecord::Base
before_validation { |change| change.name.capitalize! }
has_many :conversations, dependent: :destroy
has_many :discussions, through: :conversations
has_many :groups, through: :discussions
validates :name, presence: true, uniqueness: true
def self.top_ranked(options = {})
top.limit(1).first
end
def self.top(options = {})
with_rank(options).order("score desc")
end
def self.with_rank(options = {})
select("changes.*, ROUND((AVG(11 - conversations.rank) + COUNT(conversations.id)), 1) as score").group(options[:group_by] ? options[:group_by] : "changes.id").joins(:conversations)
end
def self.with_ten_seed(options = {})
select("changes.*, ROUND(AVG(conversations.ten_seed), 1) as average_ten_seed").group("changes.id").joins(:conversations)
end
end
Started GET "/national_offices" for 127.0.0.1 at 2013-12-17 16:52:37 +0000
Processing by NationalOfficesController#index as HTML
NationalOffice Load (0.8ms) SELECT "national_offices".* FROM "national_offices"
(0.6ms) SELECT COUNT(*) FROM "programmes" WHERE "programmes"."national_office_id" = $1 [["national_office_id", 1]]
Rendered national_offices/index.html.erb within layouts/application (10.9ms)
Completed 500 Internal Server Error in 14ms
ActionView::Template::Error (undefined method `[]' for false:FalseClass):
13: <tr>
14: <td><%= link_to national_office.name, national_office %></td>
15: <td><%= national_office.programmes.count %></td>
16: <td><%= national_office.top_change.respond_to?(:name) ? national_office.top_change.name : "N/A" %></td>
17: </tr>
18: <% end %>
19: </tbody>
app/models/change.rb:21:in `with_rank'
app/models/change.rb:17:in `top'
app/models/national_office.rb:10:in `top_change'
app/views/national_offices/index.html.erb:16:in `block in _app_views_national_offices_index_html_erb___842741853516102737_69950254428600'
app/views/national_offices/index.html.erb:12:in `_app_views_national_offices_index_html_erb___842741853516102737_69950254428600'
Rendered /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.5ms)
Rendered /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.5ms)
Rendered /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (12.2ms)
<p class="guidance">National Offices</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Number of Programmes</th>
<th>Top Change</th>
</tr>
</thead>
<tbody>
<% @national_offices.each do |national_office| %>
<tr>
<td><%= link_to national_office.name, national_office %></td>
<td><%= national_office.programmes.count %></td>
<td><%= national_office.top_change.respond_to?(:name) ? national_office.top_change.name : "N/A" %></td>
</tr>
<% end %>
</tbody>
</table>
class NationalOffice < ActiveRecord::Base
belongs_to :regional_office
has_many :programmes, dependent: :destroy
has_many :groups, through: :programmes
has_many :changes, through: :programmes
validates :name, :regional_office_id, presence: true
def top_change
changes.merge(Change.top(false)).take
end
def changes_with_score
changes.merge Change.with_ten_seed.top
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment