aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/railtie.rb
diff options
context:
space:
mode:
authorEugene Kenny <elkenny@gmail.com>2018-09-24 03:20:35 +0100
committerEugene Kenny <elkenny@gmail.com>2018-09-24 03:20:35 +0100
commit3b954786e7876ad63c917ed1dae310624c8ccf33 (patch)
tree530548327316cf6c134b372fccc4dd820469ab49 /activerecord/lib/active_record/railtie.rb
parentbaffadaec9200d6653089f4937bfe53f1c0e127c (diff)
downloadrails-3b954786e7876ad63c917ed1dae310624c8ccf33.tar.gz
rails-3b954786e7876ad63c917ed1dae310624c8ccf33.tar.bz2
rails-3b954786e7876ad63c917ed1dae310624c8ccf33.zip
Eagerly define attribute methods in production
The attribute methods for a model are currently defined lazily the first time that model is instantiated, even when `config.eager_load` is true. This means the first request to use each model incurs the cost, which usually involves a database round trip to fetch the schema definition. By defining the attribute methods for all models while the application is booting, we move that work out of any individual request. When using a forking web server, this also reduces the number of times the schema definition is queried by doing it once in the parent process instead of from each forked process during their first request.
Diffstat (limited to 'activerecord/lib/active_record/railtie.rb')
-rw-r--r--activerecord/lib/active_record/railtie.rb8
1 files changed, 8 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb
index 812fecbf32..81ad9ef3a2 100644
--- a/activerecord/lib/active_record/railtie.rb
+++ b/activerecord/lib/active_record/railtie.rb
@@ -137,6 +137,14 @@ end_error
end
end
+ initializer "active_record.define_attribute_methods" do |app|
+ config.after_initialize do
+ ActiveSupport.on_load(:active_record) do
+ descendants.each(&:define_attribute_methods) if app.config.eager_load
+ end
+ end
+ end
+
initializer "active_record.warn_on_records_fetched_greater_than" do
if config.active_record.warn_on_records_fetched_greater_than
ActiveSupport.on_load(:active_record) do