aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/railtie.rb
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2019-01-03 15:03:06 +0100
committerGitHub <noreply@github.com>2019-01-03 15:03:06 +0100
commit6f0cda8f8e208143cbd3b39e786521c2e5cddb7a (patch)
tree1156f9d508d8608c348a0c0b2ec12bb14c93c462 /activerecord/lib/active_record/railtie.rb
parent2584762cd9876780929098ad847ccf53aa45c10e (diff)
parent0007501669879a8560090897f78cf1d1b69bf30d (diff)
downloadrails-6f0cda8f8e208143cbd3b39e786521c2e5cddb7a.tar.gz
rails-6f0cda8f8e208143cbd3b39e786521c2e5cddb7a.tar.bz2
rails-6f0cda8f8e208143cbd3b39e786521c2e5cddb7a.zip
Merge pull request #33985 from eugeneius/attribute_methods_schema_cache
Only define attribute methods from schema cache
Diffstat (limited to 'activerecord/lib/active_record/railtie.rb')
-rw-r--r--activerecord/lib/active_record/railtie.rb14
1 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb
index 538659d6bd..2ee6119158 100644
--- a/activerecord/lib/active_record/railtie.rb
+++ b/activerecord/lib/active_record/railtie.rb
@@ -140,7 +140,19 @@ end_error
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
+ if app.config.eager_load
+ descendants.each do |model|
+ # SchemaMigration and InternalMetadata both override `table_exists?`
+ # to bypass the schema cache, so skip them to avoid the extra queries.
+ next if model._internal?
+
+ # If there's no connection yet, or the schema cache doesn't have the columns
+ # hash for the model cached, `define_attribute_methods` would trigger a query.
+ next unless model.connected? && model.connection.schema_cache.columns_hash?(model.table_name)
+
+ model.define_attribute_methods
+ end
+ end
end
end
end