From 597abcd62e847befca83eb5e9c2148860177b98e Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Thu, 23 Sep 2021 10:19:32 -0700 Subject: [PATCH 1/2] Use _load_from_sql instead of send(:instantiate) These are both private APIs, but _load_from_sql will give us even more standard behaviour (this is what find_by_sql calls) including emitting the instantiation.active_record notification. This may be faster in some cases since _load_from_sql includes a fast path for when an inheritance column isn't present. --- lib/github/sql.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/github/sql.rb b/lib/github/sql.rb index a004b74..e59144d 100644 --- a/lib/github/sql.rb +++ b/lib/github/sql.rb @@ -211,9 +211,11 @@ def models(klass) return [] if frozen? # Use select_all to retrieve hashes for each row instead of arrays of values. - @models = connection. - select_all(query, "#{klass.name} Load via #{self.class.name}"). - map { |record| klass.send :instantiate, record } + result_set = connection. + select_all(query, "#{klass.name} Load via #{self.class.name}") + + # This is a private Rails API and should ideally not be used + @models = klass._load_from_sql(result_set) retrieve_found_row_count freeze From 8a06b48b5f7bc06ff2d251d4cbb891d327a9e690 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Thu, 23 Sep 2021 11:10:16 -0700 Subject: [PATCH 2/2] Actually just use find_by_sql --- lib/github/sql.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/github/sql.rb b/lib/github/sql.rb index e59144d..ca94ed4 100644 --- a/lib/github/sql.rb +++ b/lib/github/sql.rb @@ -210,12 +210,7 @@ def models(klass) return @models if defined? @models return [] if frozen? - # Use select_all to retrieve hashes for each row instead of arrays of values. - result_set = connection. - select_all(query, "#{klass.name} Load via #{self.class.name}") - - # This is a private Rails API and should ideally not be used - @models = klass._load_from_sql(result_set) + @models = klass.find_by_sql(query) retrieve_found_row_count freeze