aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorEileen M. Uchitelle <eileencodes@users.noreply.github.com>2018-11-27 08:19:50 -0500
committerGitHub <noreply@github.com>2018-11-27 08:19:50 -0500
commitb9c7305dbe57931a153a540d49ae5d469af61a14 (patch)
treef0b9267ab5ea219a043c5de8da5caf5f97648d91 /activerecord/lib
parentb566be2349053198604e683e6276222ac9b16ed6 (diff)
parent3b9982a3b778b63488975eb03592d33aa9fb04dd (diff)
downloadrails-b9c7305dbe57931a153a540d49ae5d469af61a14.tar.gz
rails-b9c7305dbe57931a153a540d49ae5d469af61a14.tar.bz2
rails-b9c7305dbe57931a153a540d49ae5d469af61a14.zip
Merge pull request #34480 from tekin/configurable-implicit-ordering-column
Make it possible to override the implicit order column
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/model_schema.rb16
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb4
2 files changed, 18 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index c50a420432..55fc58e339 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -102,6 +102,21 @@ module ActiveRecord
# If true, the default table name for a Product class will be "products". If false, it would just be "product".
# See table_name for the full rules on table/class naming. This is true, by default.
+ ##
+ # :singleton-method: implicit_order_column
+ # :call-seq: implicit_order_column
+ #
+ # The name of the column records are ordered by if no explicit order clause
+ # is used during an ordered finder call. If not set the primary key is used.
+
+ ##
+ # :singleton-method: implicit_order_column=
+ # :call-seq: implicit_order_column=(column_name)
+ #
+ # Sets the column to sort records by when no explicit order clause is used
+ # during an ordered finder call. Useful when the primary key is not an
+ # auto-incrementing integer, for example when it's a UUID. Note that using
+ # a non-unique column can result in non-deterministic results.
included do
mattr_accessor :primary_key_prefix_type, instance_writer: false
@@ -110,6 +125,7 @@ module ActiveRecord
class_attribute :schema_migrations_table_name, instance_accessor: false, default: "schema_migrations"
class_attribute :internal_metadata_table_name, instance_accessor: false, default: "ar_internal_metadata"
class_attribute :pluralize_table_names, instance_writer: false, default: true
+ class_attribute :implicit_order_column, instance_accessor: false
self.protected_environments = ["production"]
self.inheritance_column = "type"
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index afaa900442..dc03b196f4 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -550,8 +550,8 @@ module ActiveRecord
end
def ordered_relation
- if order_values.empty? && primary_key
- order(arel_attribute(primary_key).asc)
+ if order_values.empty? && (implicit_order_column || primary_key)
+ order(arel_attribute(implicit_order_column || primary_key).asc)
else
self
end