Skip to content

Commit

Permalink
Use activerecord-has_some_of_many (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon authored Jul 30, 2024
1 parent 316bce0 commit 17271a1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ruby File.read(File.join(File.dirname(__FILE__), '.ruby-version')).strip
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gem 'activerecord-explain-analyze'
gem 'activerecord-has_some_of_many', github: 'bensheldon/activerecord-has_some_of_many', branch: 'main'
gem 'activerecord-postgis-adapter'
gem 'autoprefixer-rails'
gem 'aws-sdk-s3'
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
GIT
remote: https://github.com/bensheldon/activerecord-has_some_of_many.git
revision: 6e0fec013f760ef6b2408b3b18bdc161a3ddef87
branch: main
specs:
activerecord-has_some_of_many (1.0.0)
rails (>= 7.0.0.alpha)

GIT
remote: https://github.com/bensheldon/open311.git
revision: dbead4703063d070fa3615fc371d352e3a312ee2
Expand Down Expand Up @@ -493,6 +501,7 @@ PLATFORMS

DEPENDENCIES
activerecord-explain-analyze
activerecord-has_some_of_many!
activerecord-postgis-adapter
annotate
autoprefixer-rails
Expand Down
4 changes: 2 additions & 2 deletions app/models/city.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class City < ApplicationRecord
has_many :service_definitions
has_many :statuses

has_one :service_list_status, -> { latest_by_city(:service_list) }, class_name: 'Status', inverse_of: :city
has_one :service_requests_status, -> { latest_by_city(:service_requests) }, class_name: 'Status', inverse_of: :city
has_one_of_many :service_list_status, -> { service_list.order(created_at: :desc) }, class_name: 'Status', inverse_of: :city
has_one_of_many :service_requests_status, -> { service_requests.order(created_at: :desc) }, class_name: 'Status', inverse_of: :city
has_many :service_list_statuses, -> { service_list }, class_name: 'Status', inverse_of: :city
has_many :service_requests_statuses, -> { service_requests }, class_name: 'Status', inverse_of: :city
has_many :service_list_status_errors, lambda {
Expand Down
23 changes: 0 additions & 23 deletions app/models/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,6 @@
class Status < ApplicationRecord
belongs_to :city

scope :latest_by_city, lambda { |request_name, count: 1|
# This scope is complex. The table_reference (which is `cities`) is aliased to be
# the model's table (`statuses`) so that when this scope is used as in an association
# the eagerloaded foreign key query (`WHERE statuses.city_id` IN ...) will work properly.
#
# SELECT <columns>
# FROM <table reference>
# JOIN LATERAL <subquery>
# ON TRUE;
#
query = select('subquery.*').from('(SELECT *, id AS city_id FROM cities) AS statuses')

join_sql = <<~SQL.squish
JOIN LATERAL (
SELECT * FROM statuses AS sub_statuses
WHERE sub_statuses.city_id = statuses.city_id
AND sub_statuses.request_name = :request_name
ORDER BY created_at DESC LIMIT :count
) AS subquery ON TRUE
SQL

query.joins(sanitize_sql_array([join_sql, { request_name:, count: }]))
}
scope :service_list, -> { where(request_name: 'service_list') }
scope :service_requests, -> { where(request_name: 'service_requests') }
scope :errored, -> { where(http_code: 400..) }
Expand Down

0 comments on commit 17271a1

Please sign in to comment.