aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur Nogueira Neves <github@arthurnn.com>2016-03-24 17:17:10 -0400
committerArthur Nogueira Neves <github@arthurnn.com>2016-03-24 17:17:10 -0400
commita12ad8ae542d0d96249be7db6f708ead4d7054aa (patch)
treed5948f4361fb0da1d09c92ce2b9b33c6fd9ee749
parentba06dab5451cdce9c2264abd6c9dd20cb2722c8d (diff)
parentb6d1f68412054150644d8b91734654ffb1c64521 (diff)
downloadrails-a12ad8ae542d0d96249be7db6f708ead4d7054aa.tar.gz
rails-a12ad8ae542d0d96249be7db6f708ead4d7054aa.tar.bz2
rails-a12ad8ae542d0d96249be7db6f708ead4d7054aa.zip
Merge pull request #24292 from arthurnn/arthurnn/pk_sequence_klass
Move sequence value methods to Model level
-rw-r--r--activerecord/lib/active_record/model_schema.rb12
-rw-r--r--activerecord/lib/active_record/relation.rb4
2 files changed, 14 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index ee52c3ae02..52eab952e1 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -231,6 +231,18 @@ module ActiveRecord
@explicit_sequence_name = true
end
+ # Determines if the primary key values should be selected from their
+ # corresponding sequence before the insert statement.
+ def prefetch_primary_key?
+ connection.prefetch_primary_key?(table_name)
+ end
+
+ # Returns the next value that will be used as the primary key on
+ # an insert statment.
+ def next_sequence_value
+ connection.next_sequence_value(sequence_name)
+ end
+
# Indicates whether the table associated with this class exists
def table_exists?
connection.schema_cache.data_source_exists?(table_name)
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 09afdc6c69..777b593812 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -45,8 +45,8 @@ module ActiveRecord
k.name == primary_key
}]
- if !primary_key_value && connection.prefetch_primary_key?(klass.table_name)
- primary_key_value = connection.next_sequence_value(klass.sequence_name)
+ if !primary_key_value && klass.prefetch_primary_key?
+ primary_key_value = klass.next_sequence_value
values[arel_attribute(klass.primary_key)] = primary_key_value
end
end