aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBen Toews <mastahyeti@gmail.com>2017-09-25 10:58:08 -0600
committerMatthew Draper <matthew@trebex.net>2017-11-09 22:39:48 +1030
commit02a17492a937d4b423590644ad1481b82facd394 (patch)
treefe6a27b94d8e2ae46f9f4f1bd1a53b3c0f7f1ff5
parent40d302d880f247ef9547708b1d26a390945b6fe9 (diff)
downloadrails-02a17492a937d4b423590644ad1481b82facd394.tar.gz
rails-02a17492a937d4b423590644ad1481b82facd394.tar.bz2
rails-02a17492a937d4b423590644ad1481b82facd394.zip
work around deprecation warnings in a bunch of tests
-rw-r--r--activerecord/test/cases/associations/cascaded_eager_loading_test.rb32
-rw-r--r--activerecord/test/cases/associations/eager_load_nested_include_test.rb2
-rw-r--r--activerecord/test/cases/associations/eager_test.rb104
-rw-r--r--activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb2
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb10
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb4
-rw-r--r--activerecord/test/cases/associations/has_one_through_associations_test.rb6
-rw-r--r--activerecord/test/cases/associations/inverse_associations_test.rb8
-rw-r--r--activerecord/test/cases/associations/join_model_test.rb32
-rw-r--r--activerecord/test/cases/associations/nested_through_associations_test.rb30
-rw-r--r--activerecord/test/cases/base_test.rb24
-rw-r--r--activerecord/test/cases/batches_test.rb14
-rw-r--r--activerecord/test/cases/finder_test.rb40
-rw-r--r--activerecord/test/cases/relation/merging_test.rb14
-rw-r--r--activerecord/test/cases/relation/mutation_test.rb2
-rw-r--r--activerecord/test/cases/relation/or_test.rb14
-rw-r--r--activerecord/test/cases/relations_test.rb125
-rw-r--r--activerecord/test/cases/scoping/default_scoping_test.rb62
-rw-r--r--activerecord/test/cases/scoping/named_scoping_test.rb4
-rw-r--r--activerecord/test/cases/scoping/relation_scoping_test.rb16
-rw-r--r--activerecord/test/models/author.rb24
-rw-r--r--activerecord/test/models/car.rb6
-rw-r--r--activerecord/test/models/category.rb2
-rw-r--r--activerecord/test/models/comment.rb2
-rw-r--r--activerecord/test/models/company.rb2
-rw-r--r--activerecord/test/models/company_in_module.rb2
-rw-r--r--activerecord/test/models/developer.rb22
-rw-r--r--activerecord/test/models/membership.rb2
-rw-r--r--activerecord/test/models/owner.rb2
-rw-r--r--activerecord/test/models/person.rb2
-rw-r--r--activerecord/test/models/pirate.rb4
-rw-r--r--activerecord/test/models/post.rb12
-rw-r--r--activerecord/test/models/project.rb4
-rw-r--r--activerecord/test/models/tag.rb2
34 files changed, 320 insertions, 313 deletions
diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb
index e69cfe5e52..3eba5ed466 100644
--- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb
+++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb
@@ -18,7 +18,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
:categorizations, :people, :categories, :edges, :vertices
def test_eager_association_loading_with_cascaded_two_levels
- authors = Author.all.merge!(includes: { posts: :comments }, order: "authors.id").to_a
+ authors = Author.all.merge!(includes: { posts: :comments }, order: Arel.sql("authors.id")).to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
@@ -26,7 +26,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
end
def test_eager_association_loading_with_cascaded_two_levels_and_one_level
- authors = Author.all.merge!(includes: [{ posts: :comments }, :categorizations], order: "authors.id").to_a
+ authors = Author.all.merge!(includes: [{ posts: :comments }, :categorizations], order: Arel.sql("authors.id")).to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
@@ -42,7 +42,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
end
def test_eager_association_loading_grafts_stashed_associations_to_correct_parent
- assert_equal people(:michael), Person.eager_load(primary_contact: :primary_contact).where("primary_contacts_people_2.first_name = ?", "Susan").order("people.id").first
+ assert_equal people(:michael), Person.eager_load(primary_contact: :primary_contact).where("primary_contacts_people_2.first_name = ?", "Susan").order(Arel.sql("people.id")).first
end
def test_cascaded_eager_association_loading_with_join_for_count
@@ -78,7 +78,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
end
def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
- authors = Author.all.merge!(includes: { posts: [:comments, :categorizations] }, order: "authors.id").to_a
+ authors = Author.all.merge!(includes: { posts: [:comments, :categorizations] }, order: Arel.sql("authors.id")).to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
@@ -86,7 +86,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
end
def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference
- authors = Author.all.merge!(includes: { posts: [:comments, :author] }, order: "authors.id").to_a
+ authors = Author.all.merge!(includes: { posts: [:comments, :author] }, order: Arel.sql("authors.id")).to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal authors(:david).name, authors[0].name
@@ -94,13 +94,13 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
end
def test_eager_association_loading_with_cascaded_two_levels_with_condition
- authors = Author.all.merge!(includes: { posts: :comments }, where: "authors.id=1", order: "authors.id").to_a
+ authors = Author.all.merge!(includes: { posts: :comments }, where: "authors.id=1", order: Arel.sql("authors.id")).to_a
assert_equal 1, authors.size
assert_equal 5, authors[0].posts.size
end
def test_eager_association_loading_with_cascaded_three_levels_by_ping_pong
- firms = Firm.all.merge!(includes: { account: { firm: :account } }, order: "companies.id").to_a
+ firms = Firm.all.merge!(includes: { account: { firm: :account } }, order: Arel.sql("companies.id")).to_a
assert_equal 2, firms.size
assert_equal firms.first.account, firms.first.account.firm.account
assert_equal companies(:first_firm).account, assert_no_queries { firms.first.account.firm.account }
@@ -108,7 +108,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
end
def test_eager_association_loading_with_has_many_sti
- topics = Topic.all.merge!(includes: :replies, order: "topics.id").to_a
+ topics = Topic.all.merge!(includes: :replies, order: Arel.sql("topics.id")).to_a
first, second, = topics(:first).replies.size, topics(:second).replies.size
assert_no_queries do
assert_equal first, topics[0].replies.size
@@ -121,7 +121,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
silly.parent_id = 1
assert silly.save
- topics = Topic.all.merge!(includes: :replies, order: ["topics.id", "replies_topics.id"]).to_a
+ topics = Topic.all.merge!(includes: :replies, order: [Arel.sql("topics.id"), Arel.sql("replies_topics.id")]).to_a
assert_no_queries do
assert_equal 2, topics[0].replies.size
assert_equal 0, topics[1].replies.size
@@ -129,14 +129,14 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
end
def test_eager_association_loading_with_belongs_to_sti
- replies = Reply.all.merge!(includes: :topic, order: "topics.id").to_a
+ replies = Reply.all.merge!(includes: :topic, order: Arel.sql("topics.id")).to_a
assert_includes replies, topics(:second)
assert_not_includes replies, topics(:first)
assert_equal topics(:first), assert_no_queries { replies.first.topic }
end
def test_eager_association_loading_with_multiple_stis_and_order
- author = Author.all.merge!(includes: { posts: [ :special_comments , :very_special_comment ] }, order: ["authors.name", "comments.body", "very_special_comments_posts.body"], where: "posts.id = 4").first
+ author = Author.all.merge!(includes: { posts: [ :special_comments , :very_special_comment ] }, order: [Arel.sql("authors.name"), Arel.sql("comments.body"), Arel.sql("very_special_comments_posts.body")], where: "posts.id = 4").first
assert_equal authors(:david), author
assert_no_queries do
author.posts.first.special_comments
@@ -145,7 +145,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
end
def test_eager_association_loading_of_stis_with_multiple_references
- authors = Author.all.merge!(includes: { posts: { special_comments: { post: [ :special_comments, :very_special_comment ] } } }, order: "comments.body, very_special_comments_posts.body", where: "posts.id = 4").to_a
+ authors = Author.all.merge!(includes: { posts: { special_comments: { post: [ :special_comments, :very_special_comment ] } } }, order: Arel.sql("comments.body, very_special_comments_posts.body"), where: "posts.id = 4").to_a
assert_equal [authors(:david)], authors
assert_no_queries do
authors.first.posts.first.special_comments.first.post.special_comments
@@ -154,7 +154,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
end
def test_eager_association_loading_where_first_level_returns_nil
- authors = Author.all.merge!(includes: { post_about_thinking: :comments }, order: "authors.id DESC").to_a
+ authors = Author.all.merge!(includes: { post_about_thinking: :comments }, order: Arel.sql("authors.id DESC")).to_a
assert_equal [authors(:bob), authors(:mary), authors(:david)], authors
assert_no_queries do
authors[2].post_about_thinking.comments.first
@@ -162,17 +162,17 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
end
def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
- source = Vertex.all.merge!(includes: { sinks: { sinks: { sinks: :sinks } } }, order: "vertices.id").first
+ source = Vertex.all.merge!(includes: { sinks: { sinks: { sinks: :sinks } } }, order: Arel.sql("vertices.id")).first
assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first }
end
def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many
- sink = Vertex.all.merge!(includes: { sources: { sources: { sources: :sources } } }, order: "vertices.id DESC").first
+ sink = Vertex.all.merge!(includes: { sources: { sources: { sources: :sources } } }, order: Arel.sql("vertices.id DESC")).first
assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first }
end
def test_eager_association_loading_with_cascaded_interdependent_one_level_and_two_levels
- authors_relation = Author.all.merge!(includes: [:comments, { posts: :categorizations }], order: "authors.id")
+ authors_relation = Author.all.merge!(includes: [:comments, { posts: :categorizations }], order: Arel.sql("authors.id"))
authors = authors_relation.to_a
assert_equal 3, authors.size
assert_equal 10, authors[0].comments.size
diff --git a/activerecord/test/cases/associations/eager_load_nested_include_test.rb b/activerecord/test/cases/associations/eager_load_nested_include_test.rb
index c5b2b77bd4..b1809401fb 100644
--- a/activerecord/test/cases/associations/eager_load_nested_include_test.rb
+++ b/activerecord/test/cases/associations/eager_load_nested_include_test.rb
@@ -120,7 +120,7 @@ class EagerLoadNestedIncludeWithMissingDataTest < ActiveRecord::TestCase
assert_nothing_raised do
# @davey_mcdave doesn't have any author_favorites
includes = { posts: :comments, categorizations: :category, author_favorites: :favorite_author }
- Author.all.merge!(includes: includes, where: { authors: { name: @davey_mcdave.name } }, order: "categories.name").to_a
+ Author.all.merge!(includes: includes, where: { authors: { name: @davey_mcdave.name } }, order: Arel.sql("categories.name")).to_a
end
end
end
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 9afe6a893c..d5ca87900e 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -62,7 +62,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_loading_with_one_association_with_non_preload
- posts = Post.all.merge!(includes: :last_comment, order: "comments.id DESC").to_a
+ posts = Post.all.merge!(includes: :last_comment, order: Arel.sql("comments.id DESC")).to_a
post = posts.find { |p| p.id == 1 }
assert_equal Post.find(1).last_comment, post.last_comment
end
@@ -82,7 +82,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_with_ordering
- list = Post.all.merge!(includes: :comments, order: "posts.id DESC").to_a
+ list = Post.all.merge!(includes: :comments, order: Arel.sql("posts.id DESC")).to_a
[:other_by_mary, :other_by_bob, :misc_by_mary, :misc_by_bob, :eager_other,
:sti_habtm, :sti_post_and_comments, :sti_comments, :authorless, :thinking, :welcome
].each_with_index do |post, index|
@@ -108,12 +108,12 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_with_two_tables_in_from_without_getting_double_quoted
- posts = Post.select("posts.*").from("authors, posts").eager_load(:comments).where("posts.author_id = authors.id").order("posts.id").to_a
+ posts = Post.select("posts.*").from("authors, posts").eager_load(:comments).where("posts.author_id = authors.id").order(Arel.sql("posts.id")).to_a
assert_equal 2, posts.first.comments.size
end
def test_loading_with_multiple_associations
- posts = Post.all.merge!(includes: [ :comments, :author, :categories ], order: "posts.id").to_a
+ posts = Post.all.merge!(includes: [ :comments, :author, :categories ], order: Arel.sql("posts.id")).to_a
assert_equal 2, posts.first.comments.size
assert_equal 2, posts.first.categories.size
assert_includes posts.first.comments, comments(:greetings)
@@ -279,7 +279,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_loading_from_an_association
- posts = authors(:david).posts.merge(includes: :comments, order: "posts.id").to_a
+ posts = authors(:david).posts.merge(includes: :comments, order: Arel.sql("posts.id")).to_a
assert_equal 2, posts.first.comments.size
end
@@ -312,17 +312,17 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_nested_loading_through_has_one_association_with_order
- aa = AuthorAddress.all.merge!(includes: { author: :posts }, order: "author_addresses.id").find(author_addresses(:david_address).id)
+ aa = AuthorAddress.all.merge!(includes: { author: :posts }, order: Arel.sql("author_addresses.id")).find(author_addresses(:david_address).id)
assert_equal aa.author.posts.count, aa.author.posts.length
end
def test_nested_loading_through_has_one_association_with_order_on_association
- aa = AuthorAddress.all.merge!(includes: { author: :posts }, order: "authors.id").find(author_addresses(:david_address).id)
+ aa = AuthorAddress.all.merge!(includes: { author: :posts }, order: Arel.sql("authors.id")).find(author_addresses(:david_address).id)
assert_equal aa.author.posts.count, aa.author.posts.length
end
def test_nested_loading_through_has_one_association_with_order_on_nested_association
- aa = AuthorAddress.all.merge!(includes: { author: :posts }, order: "posts.id").find(author_addresses(:david_address).id)
+ aa = AuthorAddress.all.merge!(includes: { author: :posts }, order: Arel.sql("posts.id")).find(author_addresses(:david_address).id)
assert_equal aa.author.posts.count, aa.author.posts.length
end
@@ -364,31 +364,31 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_eager_association_loading_with_belongs_to_and_limit
- comments = Comment.all.merge!(includes: :post, limit: 5, order: "comments.id").to_a
+ comments = Comment.all.merge!(includes: :post, limit: 5, order: Arel.sql("comments.id")).to_a
assert_equal 5, comments.length
assert_equal [1, 2, 3, 5, 6], comments.collect(&:id)
end
def test_eager_association_loading_with_belongs_to_and_limit_and_conditions
- comments = Comment.all.merge!(includes: :post, where: "post_id = 4", limit: 3, order: "comments.id").to_a
+ comments = Comment.all.merge!(includes: :post, where: "post_id = 4", limit: 3, order: Arel.sql("comments.id")).to_a
assert_equal 3, comments.length
assert_equal [5, 6, 7], comments.collect(&:id)
end
def test_eager_association_loading_with_belongs_to_and_limit_and_offset
- comments = Comment.all.merge!(includes: :post, limit: 3, offset: 2, order: "comments.id").to_a
+ comments = Comment.all.merge!(includes: :post, limit: 3, offset: 2, order: Arel.sql("comments.id")).to_a
assert_equal 3, comments.length
assert_equal [3, 5, 6], comments.collect(&:id)
end
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions
- comments = Comment.all.merge!(includes: :post, where: "post_id = 4", limit: 3, offset: 1, order: "comments.id").to_a
+ comments = Comment.all.merge!(includes: :post, where: "post_id = 4", limit: 3, offset: 1, order: Arel.sql("comments.id")).to_a
assert_equal 3, comments.length
assert_equal [6, 7, 8], comments.collect(&:id)
end
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
- comments = Comment.all.merge!(includes: :post, where: ["post_id = ?", 4], limit: 3, offset: 1, order: "comments.id").to_a
+ comments = Comment.all.merge!(includes: :post, where: ["post_id = ?", 4], limit: 3, offset: 1, order: Arel.sql("comments.id")).to_a
assert_equal 3, comments.length
assert_equal [6, 7, 8], comments.collect(&:id)
end
@@ -402,7 +402,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_eager_association_loading_with_belongs_to_and_conditions_hash
comments = []
assert_nothing_raised do
- comments = Comment.all.merge!(includes: :post, where: { posts: { id: 4 } }, limit: 3, order: "comments.id").to_a
+ comments = Comment.all.merge!(includes: :post, where: { posts: { id: 4 } }, limit: 3, order: Arel.sql("comments.id")).to_a
end
assert_equal 3, comments.length
assert_equal [5, 6, 7], comments.collect(&:id)
@@ -420,25 +420,25 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
assert_nothing_raised do
- Comment.all.merge!(includes: :post, order: "posts.id").to_a
+ Comment.all.merge!(includes: :post, order: Arel.sql("posts.id")).to_a
end
end
def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
quoted_posts_id = Comment.connection.quote_table_name("posts") + "." + Comment.connection.quote_column_name("id")
assert_nothing_raised do
- Comment.includes(:post).references(:posts).order(quoted_posts_id)
+ Comment.includes(:post).references(:posts).order(Arel.sql(quoted_posts_id))
end
end
def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
- posts = Post.all.merge!(includes: [:author, :very_special_comment], limit: 1, order: "posts.id").to_a
+ posts = Post.all.merge!(includes: [:author, :very_special_comment], limit: 1, order: Arel.sql("posts.id")).to_a
assert_equal 1, posts.length
assert_equal [1], posts.collect(&:id)
end
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
- posts = Post.all.merge!(includes: [:author, :very_special_comment], limit: 1, offset: 1, order: "posts.id").to_a
+ posts = Post.all.merge!(includes: [:author, :very_special_comment], limit: 1, offset: 1, order: Arel.sql("posts.id")).to_a
assert_equal 1, posts.length
assert_equal [2], posts.collect(&:id)
end
@@ -508,9 +508,9 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_eager_with_has_many_through
- posts_with_comments = people(:michael).posts.merge(includes: :comments, order: "posts.id").to_a
- posts_with_author = people(:michael).posts.merge(includes: :author, order: "posts.id").to_a
- posts_with_comments_and_author = people(:michael).posts.merge(includes: [ :comments, :author ], order: "posts.id").to_a
+ posts_with_comments = people(:michael).posts.merge(includes: :comments, order: Arel.sql("posts.id")).to_a
+ posts_with_author = people(:michael).posts.merge(includes: :author, order: Arel.sql("posts.id")).to_a
+ posts_with_comments_and_author = people(:michael).posts.merge(includes: [ :comments, :author ], order: Arel.sql("posts.id")).to_a
assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum + post.comments.size }
assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
@@ -526,7 +526,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_eager_with_has_many_through_an_sti_join_model
- author = Author.all.merge!(includes: :special_post_comments, order: "authors.id").first
+ author = Author.all.merge!(includes: :special_post_comments, order: Arel.sql("authors.id")).first
assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
end
@@ -539,14 +539,14 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
- author = Author.all.merge!(includes: :special_nonexistent_post_comments, order: "authors.id").first
+ author = Author.all.merge!(includes: :special_nonexistent_post_comments, order: Arel.sql("authors.id")).first
assert_equal [], author.special_nonexistent_post_comments
end
def test_eager_with_has_many_through_join_model_with_conditions
assert_equal Author.all.merge!(includes: :hello_post_comments,
- order: "authors.id").first.hello_post_comments.sort_by(&:id),
- Author.all.merge!(order: "authors.id").first.hello_post_comments.sort_by(&:id)
+ order: Arel.sql("authors.id")).first.hello_post_comments.sort_by(&:id),
+ Author.all.merge!(order: Arel.sql("authors.id")).first.hello_post_comments.sort_by(&:id)
end
def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
@@ -573,19 +573,19 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_eager_with_has_many_and_limit
- posts = Post.all.merge!(order: "posts.id asc", includes: [ :author, :comments ], limit: 2).to_a
+ posts = Post.all.merge!(order: Arel.sql("posts.id asc"), includes: [ :author, :comments ], limit: 2).to_a
assert_equal 2, posts.size
assert_equal 3, posts.inject(0) { |sum, post| sum + post.comments.size }
end
def test_eager_with_has_many_and_limit_and_conditions
- posts = Post.all.merge!(includes: [ :author, :comments ], limit: 2, where: "posts.body = 'hello'", order: "posts.id").to_a
+ posts = Post.all.merge!(includes: [ :author, :comments ], limit: 2, where: "posts.body = 'hello'", order: Arel.sql("posts.id")).to_a
assert_equal 2, posts.size
assert_equal [4, 5], posts.collect(&:id)
end
def test_eager_with_has_many_and_limit_and_conditions_array
- posts = Post.all.merge!(includes: [ :author, :comments ], limit: 2, where: [ "posts.body = ?", "hello" ], order: "posts.id").to_a
+ posts = Post.all.merge!(includes: [ :author, :comments ], limit: 2, where: [ "posts.body = ?", "hello" ], order: Arel.sql("posts.id")).to_a
assert_equal 2, posts.size
assert_equal [4, 5], posts.collect(&:id)
end
@@ -643,7 +643,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_eager_with_has_and_belongs_to_many_and_limit
- posts = Post.all.merge!(includes: :categories, order: "posts.id", limit: 3).to_a
+ posts = Post.all.merge!(includes: :categories, order: Arel.sql("posts.id"), limit: 3).to_a
assert_equal 3, posts.size
assert_equal 2, posts[0].categories.size
assert_equal 1, posts[1].categories.size
@@ -709,7 +709,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_eager_association_loading_with_habtm
- posts = Post.all.merge!(includes: :categories, order: "posts.id").to_a
+ posts = Post.all.merge!(includes: :categories, order: Arel.sql("posts.id")).to_a
assert_equal 2, posts[0].categories.size
assert_equal 1, posts[1].categories.size
assert_equal 0, posts[2].categories.size
@@ -874,14 +874,14 @@ class EagerAssociationTest < ActiveRecord::TestCase
posts(:thinking, :sti_comments),
Post.all.merge!(
includes: [:author, :comments], where: { "authors.name" => "David" },
- order: "UPPER(posts.title)", limit: 2, offset: 1
+ order: Arel.sql("UPPER(posts.title)"), limit: 2, offset: 1
).to_a
)
assert_equal(
posts(:sti_post_and_comments, :sti_comments),
Post.all.merge!(
includes: [:author, :comments], where: { "authors.name" => "David" },
- order: "UPPER(posts.title) DESC", limit: 2, offset: 1
+ order: Arel.sql("UPPER(posts.title) DESC"), limit: 2, offset: 1
).to_a
)
end
@@ -891,14 +891,14 @@ class EagerAssociationTest < ActiveRecord::TestCase
posts(:thinking, :sti_comments),
Post.all.merge!(
includes: [:author, :comments], where: { "authors.name" => "David" },
- order: ["UPPER(posts.title)", "posts.id"], limit: 2, offset: 1
+ order: [Arel.sql("UPPER(posts.title)"), Arel.sql("posts.id")], limit: 2, offset: 1
).to_a
)
assert_equal(
posts(:sti_post_and_comments, :sti_comments),
Post.all.merge!(
includes: [:author, :comments], where: { "authors.name" => "David" },
- order: ["UPPER(posts.title) DESC", "posts.id"], limit: 2, offset: 1
+ order: [Arel.sql("UPPER(posts.title) DESC"), Arel.sql("posts.id")], limit: 2, offset: 1
).to_a
)
end
@@ -909,7 +909,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
Person.references(:number1_fans_people).merge(
includes: [:readers, :primary_contact, :number1_fan],
where: "number1_fans_people.first_name like 'M%'",
- order: "people.id", limit: 2, offset: 0
+ order: Arel.sql("people.id"), limit: 2, offset: 0
).to_a
)
end
@@ -1089,12 +1089,12 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_order_on_join_table_with_include_and_limit
- assert_equal 5, Developer.all.merge!(includes: "projects", order: "developers_projects.joined_on DESC", limit: 5).to_a.size
+ assert_equal 5, Developer.all.merge!(includes: "projects", order: Arel.sql("developers_projects.joined_on DESC"), limit: 5).to_a.size
end
def test_eager_loading_with_order_on_joined_table_preloads
posts = assert_queries(2) do
- Post.all.merge!(joins: :comments, includes: :author, order: "comments.id DESC").to_a
+ Post.all.merge!(joins: :comments, includes: :author, order: Arel.sql("comments.id DESC")).to_a
end
assert_equal posts(:eager_other), posts[1]
assert_equal authors(:mary), assert_no_queries { posts[1].author }
@@ -1102,18 +1102,18 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_eager_loading_with_conditions_on_joined_table_preloads
posts = assert_queries(2) do
- Post.all.merge!(select: "distinct posts.*", includes: :author, joins: [:comments], where: "comments.body like 'Thank you%'", order: "posts.id").to_a
+ Post.all.merge!(select: "distinct posts.*", includes: :author, joins: [:comments], where: "comments.body like 'Thank you%'", order: Arel.sql("posts.id")).to_a
end
assert_equal [posts(:welcome)], posts
assert_equal authors(:david), assert_no_queries { posts[0].author }
posts = assert_queries(2) do
- Post.all.merge!(includes: :author, joins: { taggings: :tag }, where: "tags.name = 'General'", order: "posts.id").to_a
+ Post.all.merge!(includes: :author, joins: { taggings: :tag }, where: "tags.name = 'General'", order: Arel.sql("posts.id")).to_a
end
assert_equal posts(:welcome, :thinking), posts
posts = assert_queries(2) do
- Post.all.merge!(includes: :author, joins: { taggings: { tag: :taggings } }, where: "taggings_tags.super_tag_id=2", order: "posts.id").to_a
+ Post.all.merge!(includes: :author, joins: { taggings: { tag: :taggings } }, where: "taggings_tags.super_tag_id=2", order: Arel.sql("posts.id")).to_a
end
assert_equal posts(:welcome, :thinking), posts
end
@@ -1132,13 +1132,13 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_eager_loading_with_conditions_on_string_joined_table_preloads
posts = assert_queries(2) do
- Post.all.merge!(select: "distinct posts.*", includes: :author, joins: "INNER JOIN comments on comments.post_id = posts.id", where: "comments.body like 'Thank you%'", order: "posts.id").to_a
+ Post.all.merge!(select: "distinct posts.*", includes: :author, joins: "INNER JOIN comments on comments.post_id = posts.id", where: "comments.body like 'Thank you%'", order: Arel.sql("posts.id")).to_a
end
assert_equal [posts(:welcome)], posts
assert_equal authors(:david), assert_no_queries { posts[0].author }
posts = assert_queries(2) do
- Post.all.merge!(select: "distinct posts.*", includes: :author, joins: ["INNER JOIN comments on comments.post_id = posts.id"], where: "comments.body like 'Thank you%'", order: "posts.id").to_a
+ Post.all.merge!(select: "distinct posts.*", includes: :author, joins: ["INNER JOIN comments on comments.post_id = posts.id"], where: "comments.body like 'Thank you%'", order: Arel.sql("posts.id")).to_a
end
assert_equal [posts(:welcome)], posts
assert_equal authors(:david), assert_no_queries { posts[0].author }
@@ -1146,7 +1146,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_eager_loading_with_select_on_joined_table_preloads
posts = assert_queries(2) do
- Post.all.merge!(select: "posts.*, authors.name as author_name", includes: :comments, joins: :author, order: "posts.id").to_a
+ Post.all.merge!(select: "posts.*, authors.name as author_name", includes: :comments, joins: :author, order: Arel.sql("posts.id")).to_a
end
assert_equal "David", posts[0].author_name
assert_equal posts(:welcome).comments, assert_no_queries { posts[0].comments }
@@ -1190,7 +1190,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
if current_adapter?(:OracleAdapter)
firm = Firm.all.merge!(includes: :clients_using_primary_key, order: "clients_using_primary_keys_companies"[0, 30] + ".name").find(1)
else
- firm = Firm.all.merge!(includes: :clients_using_primary_key, order: "clients_using_primary_keys_companies.name").find(1)
+ firm = Firm.all.merge!(includes: :clients_using_primary_key, order: Arel.sql("clients_using_primary_keys_companies.name")).find(1)
end
assert_no_queries do
assert_equal expected, firm.clients_using_primary_key
@@ -1199,7 +1199,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_preload_has_one_using_primary_key
expected = accounts(:signals37)
- firm = Firm.all.merge!(includes: :account_using_primary_key, order: "companies.id").first
+ firm = Firm.all.merge!(includes: :account_using_primary_key, order: Arel.sql("companies.id")).first
assert_no_queries do
assert_equal expected, firm.account_using_primary_key
end
@@ -1207,7 +1207,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_include_has_one_using_primary_key
expected = accounts(:signals37)
- firm = Firm.all.merge!(includes: :account_using_primary_key, order: "accounts.id").to_a.detect { |f| f.id == 1 }
+ firm = Firm.all.merge!(includes: :account_using_primary_key, order: Arel.sql("accounts.id")).to_a.detect { |f| f.id == 1 }
assert_no_queries do
assert_equal expected, firm.account_using_primary_key
end
@@ -1269,7 +1269,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
def test_joins_with_includes_should_preload_via_joins
- post = assert_queries(1) { Post.includes(:comments).joins(:comments).order("posts.id desc").to_a.first }
+ post = assert_queries(1) { Post.includes(:comments).joins(:comments).order(Arel.sql("posts.id desc")).to_a.first }
assert_queries(0) do
assert_not_equal 0, post.comments.to_a.count
@@ -1278,16 +1278,16 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_join_eager_with_empty_order_should_generate_valid_sql
assert_nothing_raised do
- Post.includes(:comments).order("").where(comments: { body: "Thank you for the welcome" }).first
+ Post.includes(:comments).order(Arel.sql("")).where(comments: { body: "Thank you for the welcome" }).first
end
end
def test_deep_including_through_habtm
# warm up habtm cache
- posts = Post.all.merge!(includes: { categories: :categorizations }, order: "posts.id").to_a
+ posts = Post.all.merge!(includes: { categories: :categorizations }, order: Arel.sql("posts.id")).to_a
posts[0].categories[0].categorizations.length
- posts = Post.all.merge!(includes: { categories: :categorizations }, order: "posts.id").to_a
+ posts = Post.all.merge!(includes: { categories: :categorizations }, order: Arel.sql("posts.id")).to_a
assert_no_queries { assert_equal 2, posts[0].categories[0].categorizations.length }
assert_no_queries { assert_equal 1, posts[0].categories[1].categorizations.length }
assert_no_queries { assert_equal 2, posts[1].categories[0].categorizations.length }
@@ -1382,7 +1382,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
test "preloading associations with string joins and order references" do
author = assert_queries(2) {
- Author.includes(:posts).joins("LEFT JOIN posts ON posts.author_id = authors.id").order("posts.title DESC").first
+ Author.includes(:posts).joins("LEFT JOIN posts ON posts.author_id = authors.id").order(Arel.sql("posts.title DESC")).first
}
assert_no_queries {
assert_equal 5, author.posts.size
@@ -1513,6 +1513,6 @@ class EagerAssociationTest < ActiveRecord::TestCase
private
def find_all_ordered(klass, include = nil)
- klass.order("#{klass.table_name}.#{klass.primary_key}").includes(include).to_a
+ klass.order(Arel.sql("#{klass.table_name}.#{klass.primary_key}")).includes(include).to_a
end
end
diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
index c817d7267b..7e5655bb08 100644
--- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -587,7 +587,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
end
def test_find_should_append_to_association_order
- ordered_developers = projects(:active_record).developers.order("projects.id")
+ ordered_developers = projects(:active_record).developers.order(Arel.sql("projects.id"))
assert_equal ["developers.name desc, developers.id desc", "projects.id"], ordered_developers.order_values
end
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 6bd11a5d81..85733f9056 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -48,7 +48,7 @@ class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCa
author = authors(:david)
# this can fail on adapters which require ORDER BY expressions to be included in the SELECT expression
# if the reorder clauses are not correctly handled
- assert author.posts_with_comments_sorted_by_comment_id.where("comments.id > 0").reorder("posts.comments_count DESC", "posts.tags_count DESC").last
+ assert author.posts_with_comments_sorted_by_comment_id.where("comments.id > 0").reorder(Arel.sql("posts.comments_count DESC"), Arel.sql("posts.tags_count DESC")).last
end
end
@@ -550,7 +550,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
def test_find_should_append_to_association_order
- ordered_clients = companies(:first_firm).clients_sorted_desc.order("companies.id")
+ ordered_clients = companies(:first_firm).clients_sorted_desc.order(Arel.sql("companies.id"))
assert_equal ["id DESC", "companies.id"], ordered_clients.order_values
end
@@ -1123,7 +1123,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
def test_deleting_updates_counter_cache
- topic = Topic.order("id ASC").first
+ topic = Topic.order(Arel.sql("id ASC")).first
assert_equal topic.replies.to_a.size, topic.replies_count
topic.replies.delete(topic.replies.first)
@@ -1162,7 +1162,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
def test_pushing_association_updates_counter_cache
- topic = Topic.order("id ASC").first
+ topic = Topic.order(Arel.sql("id ASC")).first
reply = Reply.create!
assert_difference "topic.reload.replies_count", 1 do
@@ -1212,7 +1212,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
def test_calling_update_attributes_on_id_changes_the_counter_cache
- topic = Topic.order("id ASC").first
+ topic = Topic.order(Arel.sql("id ASC")).first
original_count = topic.replies.to_a.size
assert_equal original_count, topic.replies_count
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
index 046020e310..ed96eb54c1 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -71,7 +71,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
def self.name; "Person"; end
has_many :readers
- has_many :posts, -> { order("posts.id DESC") }, through: :readers
+ has_many :posts, -> { order(Arel.sql("posts.id DESC")) }, through: :readers
end
posts = person_prime.includes(:posts).first.posts
@@ -985,7 +985,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
end
def test_joining_has_many_through_belongs_to
- posts = Post.joins(:author_categorizations).order("posts.id").
+ posts = Post.joins(:author_categorizations).order(Arel.sql("posts.id")).
where("categorizations.id" => categorizations(:mary_thinking_sti).id)
assert_equal [posts(:eager_other), posts(:misc_by_mary), posts(:other_by_mary)], posts
diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb
index fe24c465b2..bef20b2ebe 100644
--- a/activerecord/test/cases/associations/has_one_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb
@@ -139,7 +139,7 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
def test_has_one_through_nonpreload_eagerloading
members = assert_queries(1) do
- Member.all.merge!(includes: :club, where: ["members.name = ?", "Groucho Marx"], order: "clubs.name").to_a #force fallback
+ Member.all.merge!(includes: :club, where: ["members.name = ?", "Groucho Marx"], order: Arel.sql("clubs.name")).to_a #force fallback
end
assert_equal 1, members.size
assert_not_nil assert_no_queries { members[0].club }
@@ -147,7 +147,7 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
def test_has_one_through_nonpreload_eager_loading_through_polymorphic
members = assert_queries(1) do
- Member.all.merge!(includes: :sponsor_club, where: ["members.name = ?", "Groucho Marx"], order: "clubs.name").to_a #force fallback
+ Member.all.merge!(includes: :sponsor_club, where: ["members.name = ?", "Groucho Marx"], order: Arel.sql("clubs.name")).to_a #force fallback
end
assert_equal 1, members.size
assert_not_nil assert_no_queries { members[0].sponsor_club }
@@ -156,7 +156,7 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
def test_has_one_through_nonpreload_eager_loading_through_polymorphic_with_more_than_one_through_record
Sponsor.new(sponsor_club: clubs(:crazy_club), sponsorable: members(:groucho)).save!
members = assert_queries(1) do
- Member.all.merge!(includes: :sponsor_club, where: ["members.name = ?", "Groucho Marx"], order: "clubs.name DESC").to_a #force fallback
+ Member.all.merge!(includes: :sponsor_club, where: ["members.name = ?", "Groucho Marx"], order: Arel.sql("clubs.name DESC")).to_a #force fallback
end
assert_equal 1, members.size
assert_not_nil assert_no_queries { members[0].sponsor_club }
diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb
index e13cf93dcf..c39e967569 100644
--- a/activerecord/test/cases/associations/inverse_associations_test.rb
+++ b/activerecord/test/cases/associations/inverse_associations_test.rb
@@ -223,7 +223,7 @@ class InverseHasOneTests < ActiveRecord::TestCase
f.man.name = "Mungo"
assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance"
- m = Man.all.merge!(where: { name: "Gordon" }, includes: :face, order: "faces.id").first
+ m = Man.all.merge!(where: { name: "Gordon" }, includes: :face, order: Arel.sql("faces.id")).first
f = m.face
assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
m.name = "Bongo"
@@ -329,7 +329,7 @@ class InverseHasManyTests < ActiveRecord::TestCase
assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance"
end
- m = Man.all.merge!(where: { name: "Gordon" }, includes: :interests, order: "interests.id").first
+ m = Man.all.merge!(where: { name: "Gordon" }, includes: :interests, order: Arel.sql("interests.id")).first
is = m.interests
is.each do |i|
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
@@ -554,7 +554,7 @@ class InverseBelongsToTests < ActiveRecord::TestCase
m.face.description = "pleasing"
assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance"
- f = Face.all.merge!(includes: :man, order: "men.id", where: { description: "trusting" }).first
+ f = Face.all.merge!(includes: :man, order: Arel.sql("men.id"), where: { description: "trusting" }).first
m = f.man
assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
f.description = "gormless"
@@ -637,7 +637,7 @@ class InversePolymorphicBelongsToTests < ActiveRecord::TestCase
m.polymorphic_face.description = "pleasing"
assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance"
- f = Face.all.merge!(where: { description: "confused" }, includes: :man, order: "men.id").first
+ f = Face.all.merge!(where: { description: "confused" }, includes: :man, order: Arel.sql("men.id")).first
m = f.polymorphic_man
assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same before changes to child instance"
f.description = "gormless"
diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb
index 87694b0788..8f08684820 100644
--- a/activerecord/test/cases/associations/join_model_test.rb
+++ b/activerecord/test/cases/associations/join_model_test.rb
@@ -244,8 +244,8 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
end
def test_include_has_many_through
- posts = Post.all.merge!(order: "posts.id").to_a
- posts_with_authors = Post.all.merge!(includes: :authors, order: "posts.id").to_a
+ posts = Post.all.merge!(order: Arel.sql("posts.id")).to_a
+ posts_with_authors = Post.all.merge!(includes: :authors, order: Arel.sql("posts.id")).to_a
assert_equal posts.length, posts_with_authors.length
posts.length.times do |i|
assert_equal posts[i].authors.length, assert_no_queries { posts_with_authors[i].authors.length }
@@ -269,8 +269,8 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
end
def test_include_polymorphic_has_many_through
- posts = Post.all.merge!(order: "posts.id").to_a
- posts_with_tags = Post.all.merge!(includes: :tags, order: "posts.id").to_a
+ posts = Post.all.merge!(order: Arel.sql("posts.id")).to_a
+ posts_with_tags = Post.all.merge!(includes: :tags, order: Arel.sql("posts.id")).to_a
assert_equal posts.length, posts_with_tags.length
posts.length.times do |i|
assert_equal posts[i].tags.length, assert_no_queries { posts_with_tags[i].tags.length }
@@ -278,8 +278,8 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
end
def test_include_polymorphic_has_many
- posts = Post.all.merge!(order: "posts.id").to_a
- posts_with_taggings = Post.all.merge!(includes: :taggings, order: "posts.id").to_a
+ posts = Post.all.merge!(order: Arel.sql("posts.id")).to_a
+ posts_with_taggings = Post.all.merge!(includes: :taggings, order: Arel.sql("posts.id")).to_a
assert_equal posts.length, posts_with_taggings.length
posts.length.times do |i|
assert_equal posts[i].taggings.length, assert_no_queries { posts_with_taggings[i].taggings.length }
@@ -326,7 +326,7 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
end
def test_has_many_through_with_custom_primary_key_on_has_many_source
- assert_equal [authors(:david), authors(:bob)], posts(:thinking).authors_using_custom_pk.order("authors.id")
+ assert_equal [authors(:david), authors(:bob)], posts(:thinking).authors_using_custom_pk.order(Arel.sql("authors.id"))
end
def test_belongs_to_polymorphic_with_counter_cache
@@ -383,19 +383,19 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
end
def test_has_many_through_has_many_find_all
- assert_equal comments(:greetings), authors(:david).comments.order("comments.id").to_a.first
+ assert_equal comments(:greetings), authors(:david).comments.order(Arel.sql("comments.id")).to_a.first
end
def test_has_many_through_has_many_find_all_with_custom_class
- assert_equal comments(:greetings), authors(:david).funky_comments.order("comments.id").to_a.first
+ assert_equal comments(:greetings), authors(:david).funky_comments.order(Arel.sql("comments.id")).to_a.first
end
def test_has_many_through_has_many_find_first
- assert_equal comments(:greetings), authors(:david).comments.order("comments.id").first
+ assert_equal comments(:greetings), authors(:david).comments.order(Arel.sql("comments.id")).first
end
def test_has_many_through_has_many_find_conditions
- options = { where: "comments.#{QUOTED_TYPE}='SpecialComment'", order: "comments.id" }
+ options = { where: "comments.#{QUOTED_TYPE}='SpecialComment'", order: Arel.sql("comments.id") }
assert_equal comments(:does_it_hurt), authors(:david).comments.merge(options).first
end
@@ -420,7 +420,7 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
end
def test_eager_load_has_many_through_has_many
- author = Author.all.merge!(where: ["name = ?", "David"], includes: :comments, order: "comments.id").first
+ author = Author.all.merge!(where: ["name = ?", "David"], includes: :comments, order: Arel.sql("comments.id")).first
SpecialComment.new; VerySpecialComment.new
assert_no_queries do
assert_equal [1, 2, 3, 5, 6, 7, 8, 9, 10, 12], author.comments.collect(&:id)
@@ -661,8 +661,8 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
end
def test_preload_polymorphic_has_many_through
- posts = Post.all.merge!(order: "posts.id").to_a
- posts_with_tags = Post.all.merge!(includes: :tags, order: "posts.id").to_a
+ posts = Post.all.merge!(order: Arel.sql("posts.id")).to_a
+ posts_with_tags = Post.all.merge!(includes: :tags, order: Arel.sql("posts.id")).to_a
assert_equal posts.length, posts_with_tags.length
posts.length.times do |i|
assert_equal posts[i].tags.length, assert_no_queries { posts_with_tags[i].tags.length }
@@ -688,8 +688,8 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
end
def test_preload_polymorphic_has_many
- posts = Post.all.merge!(order: "posts.id").to_a
- posts_with_taggings = Post.all.merge!(includes: :taggings, order: "posts.id").to_a
+ posts = Post.all.merge!(order: Arel.sql("posts.id")).to_a
+ posts_with_taggings = Post.all.merge!(includes: :taggings, order: Arel.sql("posts.id")).to_a
assert_equal posts.length, posts_with_taggings.length
posts.length.times do |i|
assert_equal posts[i].taggings.length, assert_no_queries { posts_with_taggings[i].taggings.length }
diff --git a/activerecord/test/cases/associations/nested_through_associations_test.rb b/activerecord/test/cases/associations/nested_through_associations_test.rb
index 65d30d011b..b8e8f48ae5 100644
--- a/activerecord/test/cases/associations/nested_through_associations_test.rb
+++ b/activerecord/test/cases/associations/nested_through_associations_test.rb
@@ -86,7 +86,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
# Through: has_many through
def test_has_many_through_has_many_through_with_has_many_source_reflection
luke, david = subscribers(:first), subscribers(:second)
- assert_equal [luke, david, david], authors(:david).subscribers.order("subscribers.nick")
+ assert_equal [luke, david, david], authors(:david).subscribers.order(Arel.sql("subscribers.nick"))
end
def test_has_many_through_has_many_through_with_has_many_source_reflection_preload
@@ -156,7 +156,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
groucho_details, other_details = member_details(:groucho), member_details(:some_other_guy)
assert_equal [groucho_details, other_details],
- members(:groucho).organization_member_details.order("member_details.id")
+ members(:groucho).organization_member_details.order(Arel.sql("member_details.id"))
end
def test_has_many_through_has_one_with_has_many_through_source_reflection_preload
@@ -171,7 +171,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
def test_has_many_through_has_one_with_has_many_through_source_reflection_preload_via_joins
assert_includes_and_joins_equal(
- Member.where("member_details.id" => member_details(:groucho).id).order("member_details.id"),
+ Member.where("member_details.id" => member_details(:groucho).id).order(Arel.sql("member_details.id")),
[members(:groucho), members(:some_other_guy)], :organization_member_details
)
@@ -187,7 +187,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
groucho_details, other_details = member_details(:groucho), member_details(:some_other_guy)
assert_equal [groucho_details, other_details],
- members(:groucho).organization_member_details_2.order("member_details.id")
+ members(:groucho).organization_member_details_2.order(Arel.sql("member_details.id"))
end
def test_has_many_through_has_one_through_with_has_many_source_reflection_preload
@@ -203,7 +203,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
def test_has_many_through_has_one_through_with_has_many_source_reflection_preload_via_joins
assert_includes_and_joins_equal(
- Member.where("member_details.id" => member_details(:groucho).id).order("member_details.id"),
+ Member.where("member_details.id" => member_details(:groucho).id).order(Arel.sql("member_details.id")),
[members(:groucho), members(:some_other_guy)], :organization_member_details_2
)
@@ -218,7 +218,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
def test_has_many_through_has_many_with_has_and_belongs_to_many_source_reflection
general, cooking = categories(:general), categories(:cooking)
- assert_equal [general, cooking], authors(:bob).post_categories.order("categories.id")
+ assert_equal [general, cooking], authors(:bob).post_categories.order(Arel.sql("categories.id"))
end
def test_has_many_through_has_many_with_has_and_belongs_to_many_source_reflection_preload
@@ -246,7 +246,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection
greetings, more = comments(:greetings), comments(:more_greetings)
- assert_equal [greetings, more], categories(:technology).post_comments.order("comments.id")
+ assert_equal [greetings, more], categories(:technology).post_comments.order(Arel.sql("comments.id"))
end
def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload
@@ -264,7 +264,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
Category.joins(:post_comments).first
assert_includes_and_joins_equal(
- Category.where("comments.id" => comments(:more_greetings).id).order("categories.id"),
+ Category.where("comments.id" => comments(:more_greetings).id).order(Arel.sql("categories.id")),
[categories(:general), categories(:technology)], :post_comments
)
end
@@ -275,7 +275,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection
greetings, more = comments(:greetings), comments(:more_greetings)
- assert_equal [greetings, more], authors(:bob).category_post_comments.order("comments.id")
+ assert_equal [greetings, more], authors(:bob).category_post_comments.order(Arel.sql("comments.id"))
end
def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection_preload
@@ -292,7 +292,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
Author.joins(:category_post_comments).first
assert_includes_and_joins_equal(
- Author.where("comments.id" => comments(:does_it_hurt).id).order("authors.id"),
+ Author.where("comments.id" => comments(:does_it_hurt).id).order(Arel.sql("authors.id")),
[authors(:david), authors(:mary)], :category_post_comments
)
end
@@ -327,7 +327,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
welcome_general, thinking_general = taggings(:welcome_general), taggings(:thinking_general)
assert_equal [welcome_general, thinking_general],
- categorizations(:david_welcome_general).post_taggings.order("taggings.id")
+ categorizations(:david_welcome_general).post_taggings.order(Arel.sql("taggings.id"))
end
def test_has_many_through_belongs_to_with_has_many_through_source_reflection_preload
@@ -341,7 +341,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
def test_has_many_through_belongs_to_with_has_many_through_source_reflection_preload_via_joins
assert_includes_and_joins_equal(
- Categorization.where("taggings.id" => taggings(:welcome_general).id).order("taggings.id"),
+ Categorization.where("taggings.id" => taggings(:welcome_general).id).order(Arel.sql("taggings.id")),
[categorizations(:david_welcome_general)], :post_taggings
)
end
@@ -411,7 +411,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
def test_distinct_has_many_through_a_has_many_through_association_on_through_reflection
author = authors(:david)
assert_equal [subscribers(:first), subscribers(:second)],
- author.distinct_subscribers.order("subscribers.nick")
+ author.distinct_subscribers.order(Arel.sql("subscribers.nick"))
end
def test_nested_has_many_through_with_a_table_referenced_multiple_times
@@ -436,7 +436,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
end
def test_has_many_through_with_foreign_key_option_on_through_reflection
- assert_equal [posts(:welcome), posts(:authorless)], people(:david).agents_posts.order("posts.id")
+ assert_equal [posts(:welcome), posts(:authorless)], people(:david).agents_posts.order(Arel.sql("posts.id"))
assert_equal [authors(:david)], references(:david_unicyclist).agents_posts_authors
references = Reference.joins(:agents_posts_authors).where("authors.id" => authors(:david).id)
@@ -444,7 +444,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase
end
def test_has_many_through_with_foreign_key_option_on_source_reflection
- assert_equal [people(:michael), people(:susan)], jobs(:unicyclist).agents.order("people.id")
+ assert_equal [people(:michael), people(:susan)], jobs(:unicyclist).agents.order(Arel.sql("people.id"))
jobs = Job.joins(:agents)
assert_equal [jobs(:unicyclist), jobs(:unicyclist)], jobs
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index f0ef522515..e2e4aa22f4 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -439,7 +439,7 @@ class BasicsTest < ActiveRecord::TestCase
if current_adapter?(:Mysql2Adapter)
def test_update_all_with_order_and_limit
- assert_equal 1, Topic.limit(1).order("id DESC").update_all(content: "bulk updated!")
+ assert_equal 1, Topic.limit(1).order(Arel.sql("id DESC")).update_all(content: "bulk updated!")
end
end
@@ -1081,11 +1081,11 @@ class BasicsTest < ActiveRecord::TestCase
def test_find_last
last = Developer.last
- assert_equal last, Developer.all.merge!(order: "id desc").first
+ assert_equal last, Developer.all.merge!(order: Arel.sql("id desc")).first
end
def test_last
- assert_equal Developer.all.merge!(order: "id desc").first, Developer.last
+ assert_equal Developer.all.merge!(order: Arel.sql("id desc")).first, Developer.last
end
def test_all
@@ -1095,27 +1095,27 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_all_with_conditions
- assert_equal Developer.all.merge!(order: "id desc").to_a, Developer.order("id desc").to_a
+ assert_equal Developer.all.merge!(order: Arel.sql("id desc")).to_a, Developer.order(Arel.sql("id desc")).to_a
end
def test_find_ordered_last
- last = Developer.all.merge!(order: "developers.salary ASC").last
- assert_equal last, Developer.all.merge!(order: "developers.salary ASC").to_a.last
+ last = Developer.all.merge!(order: Arel.sql("developers.salary ASC")).last
+ assert_equal last, Developer.all.merge!(order: Arel.sql("developers.salary ASC")).to_a.last
end
def test_find_reverse_ordered_last
- last = Developer.all.merge!(order: "developers.salary DESC").last
- assert_equal last, Developer.all.merge!(order: "developers.salary DESC").to_a.last
+ last = Developer.all.merge!(order: Arel.sql("developers.salary DESC")).last
+ assert_equal last, Developer.all.merge!(order: Arel.sql("developers.salary DESC")).to_a.last
end
def test_find_multiple_ordered_last
- last = Developer.all.merge!(order: "developers.name, developers.salary DESC").last
- assert_equal last, Developer.all.merge!(order: "developers.name, developers.salary DESC").to_a.last
+ last = Developer.all.merge!(order: Arel.sql("developers.name, developers.salary DESC")).last
+ assert_equal last, Developer.all.merge!(order: Arel.sql("developers.name, developers.salary DESC")).to_a.last
end
def test_find_keeps_multiple_order_values
- combined = Developer.all.merge!(order: "developers.name, developers.salary").to_a
- assert_equal combined, Developer.all.merge!(order: ["developers.name", "developers.salary"]).to_a
+ combined = Developer.all.merge!(order: Arel.sql("developers.name, developers.salary")).to_a
+ assert_equal combined, Developer.all.merge!(order: [Arel.sql("developers.name"), Arel.sql("developers.salary")]).to_a
end
def test_find_keeps_multiple_group_values
diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb
index be8aeed5ac..ff345d5f0e 100644
--- a/activerecord/test/cases/batches_test.rb
+++ b/activerecord/test/cases/batches_test.rb
@@ -9,7 +9,7 @@ class EachTest < ActiveRecord::TestCase
fixtures :posts, :subscribers
def setup
- @posts = Post.order("id asc")
+ @posts = Post.order(Arel.sql("id asc"))
@total = Post.count
Post.count("id") # preheat arel's table cache
end
@@ -101,7 +101,7 @@ class EachTest < ActiveRecord::TestCase
previous_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil
assert_nothing_raised do
- Post.order("comments_count DESC").find_each { |post| post }
+ Post.order(Arel.sql("comments_count DESC")).find_each { |post| post }
end
ensure
ActiveRecord::Base.logger = previous_logger
@@ -233,7 +233,7 @@ class EachTest < ActiveRecord::TestCase
end
def test_find_in_batches_should_use_any_column_as_primary_key
- nick_order_subscribers = Subscriber.order("nick asc")
+ nick_order_subscribers = Subscriber.order(Arel.sql("nick asc"))
start_nick = nick_order_subscribers.second.nick
subscribers = []
@@ -329,7 +329,7 @@ class EachTest < ActiveRecord::TestCase
end
def test_in_batches_each_record_should_be_ordered_by_id
- ids = Post.order("id ASC").pluck(:id)
+ ids = Post.order(Arel.sql("id ASC")).pluck(:id)
assert_queries(6) do
Post.in_batches(of: 2).each_record.with_index do |post, i|
assert_equal ids[i], post.id
@@ -384,7 +384,7 @@ class EachTest < ActiveRecord::TestCase
end
def test_in_batches_should_start_from_the_start_option
- post = Post.order("id ASC").where("id >= ?", 2).first
+ post = Post.order(Arel.sql("id ASC")).where("id >= ?", 2).first
assert_queries(2) do
relation = Post.in_batches(of: 1, start: 2).first
assert_equal post, relation.first
@@ -392,7 +392,7 @@ class EachTest < ActiveRecord::TestCase
end
def test_in_batches_should_end_at_the_finish_option
- post = Post.order("id DESC").where("id <= ?", 5).first
+ post = Post.order(Arel.sql("id DESC")).where("id <= ?", 5).first
assert_queries(7) do
relation = Post.in_batches(of: 1, finish: 5, load: true).reverse_each.first
assert_equal post, relation.last
@@ -451,7 +451,7 @@ class EachTest < ActiveRecord::TestCase
end
def test_in_batches_should_use_any_column_as_primary_key
- nick_order_subscribers = Subscriber.order("nick asc")
+ nick_order_subscribers = Subscriber.order(Arel.sql("nick asc"))
start_nick = nick_order_subscribers.second.nick
subscribers = []
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index d8bc917e7f..65ebfcd989 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -239,19 +239,19 @@ class FinderTest < ActiveRecord::TestCase
# Ensure +exists?+ runs without an error by excluding order value.
def test_exists_with_order
- assert_equal true, Topic.order("invalid sql here").exists?
+ assert_equal true, Topic.order(Arel.sql("invalid sql here")).exists?
end
def test_exists_with_joins
- assert_equal true, Topic.joins(:replies).where(replies_topics: { approved: true }).order("replies_topics.created_at DESC").exists?
+ assert_equal true, Topic.joins(:replies).where(replies_topics: { approved: true }).order(Arel.sql("replies_topics.created_at DESC")).exists?
end
def test_exists_with_left_joins
- assert_equal true, Topic.left_joins(:replies).where(replies_topics: { approved: true }).order("replies_topics.created_at DESC").exists?
+ assert_equal true, Topic.left_joins(:replies).where(replies_topics: { approved: true }).order(Arel.sql("replies_topics.created_at DESC")).exists?
end
def test_exists_with_eager_load
- assert_equal true, Topic.eager_load(:replies).where(replies_topics: { approved: true }).order("replies_topics.created_at DESC").exists?
+ assert_equal true, Topic.eager_load(:replies).where(replies_topics: { approved: true }).order(Arel.sql("replies_topics.created_at DESC")).exists?
end
def test_exists_with_includes_limit_and_empty_result
@@ -267,8 +267,8 @@ class FinderTest < ActiveRecord::TestCase
def test_exists_with_distinct_association_includes_limit_and_order
author = Author.first
- assert_equal false, author.unique_categorized_posts.includes(:special_comments).order("comments.tags_count DESC").limit(0).exists?
- assert_equal true, author.unique_categorized_posts.includes(:special_comments).order("comments.tags_count DESC").limit(1).exists?
+ assert_equal false, author.unique_categorized_posts.includes(:special_comments).order(Arel.sql("comments.tags_count DESC")).limit(0).exists?
+ assert_equal true, author.unique_categorized_posts.includes(:special_comments).order(Arel.sql("comments.tags_count DESC")).limit(1).exists?
end
def test_exists_should_reference_correct_aliases_while_joining_tables_of_has_many_through_association
@@ -652,7 +652,7 @@ class FinderTest < ActiveRecord::TestCase
def test_last_with_irreversible_order
assert_raises(ActiveRecord::IrreversibleOrderError) do
- Topic.order("coalesce(author_name, title)").last
+ Topic.order(Arel.sql("coalesce(author_name, title)")).last
end
end
@@ -813,9 +813,9 @@ class FinderTest < ActiveRecord::TestCase
end
def test_hash_condition_find_with_array
- p1, p2 = Post.limit(2).order("id asc").to_a
- assert_equal [p1, p2], Post.where(id: [p1, p2]).order("id asc").to_a
- assert_equal [p1, p2], Post.where(id: [p1, p2.id]).order("id asc").to_a
+ p1, p2 = Post.limit(2).order(Arel.sql("id asc")).to_a
+ assert_equal [p1, p2], Post.where(id: [p1, p2]).order(Arel.sql("id asc")).to_a
+ assert_equal [p1, p2], Post.where(id: [p1, p2.id]).order(Arel.sql("id asc")).to_a
end
def test_hash_condition_find_with_nil
@@ -1013,7 +1013,7 @@ class FinderTest < ActiveRecord::TestCase
end
def test_find_by_one_attribute_with_several_options
- assert_equal accounts(:unknown), Account.order("id DESC").where("id != ?", 3).find_by_credit_limit(50)
+ assert_equal accounts(:unknown), Account.order(Arel.sql("id DESC")).where("id != ?", 3).find_by_credit_limit(50)
end
def test_find_by_one_missing_attribute
@@ -1041,7 +1041,7 @@ class FinderTest < ActiveRecord::TestCase
assert_equal devs[2], Developer.offset(2).first
assert_equal devs[-3], Developer.offset(2).last
- assert_equal devs[-3], Developer.offset(2).order("id DESC").first
+ assert_equal devs[-3], Developer.offset(2).order(Arel.sql("id DESC")).first
end
def test_find_by_nil_attribute
@@ -1094,9 +1094,9 @@ class FinderTest < ActiveRecord::TestCase
end
def test_find_by_records
- p1, p2 = Post.limit(2).order("id asc").to_a
- assert_equal [p1, p2], Post.where(["id in (?)", [p1, p2]]).order("id asc")
- assert_equal [p1, p2], Post.where(["id in (?)", [p1, p2.id]]).order("id asc")
+ p1, p2 = Post.limit(2).order(Arel.sql("id asc")).to_a
+ assert_equal [p1, p2], Post.where(["id in (?)", [p1, p2]]).order(Arel.sql("id asc"))
+ assert_equal [p1, p2], Post.where(["id in (?)", [p1, p2.id]]).order(Arel.sql("id asc"))
end
def test_select_value
@@ -1125,18 +1125,18 @@ class FinderTest < ActiveRecord::TestCase
def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
assert_equal 2, Post.includes(authors: :author_address).
where.not(author_addresses: { id: nil }).
- order("author_addresses.id DESC").limit(2).to_a.size
+ order(Arel.sql("author_addresses.id DESC")).limit(2).to_a.size
assert_equal 3, Post.includes(author: :author_address, authors: :author_address).
where.not(author_addresses_authors: { id: nil }).
- order("author_addresses_authors.id DESC").limit(3).to_a.size
+ order(Arel.sql("author_addresses_authors.id DESC")).limit(3).to_a.size
end
def test_find_with_nil_inside_set_passed_for_one_attribute
client_of = Company.
where(client_of: [2, 1, nil],
name: ["37signals", "Summit", "Microsoft"]).
- order("client_of DESC").
+ order(Arel.sql("client_of DESC")).
map(&:client_of)
assert_includes client_of, nil
@@ -1146,7 +1146,7 @@ class FinderTest < ActiveRecord::TestCase
def test_find_with_nil_inside_set_passed_for_attribute
client_of = Company.
where(client_of: [nil]).
- order("client_of DESC").
+ order(Arel.sql("client_of DESC")).
map(&:client_of)
assert_equal [], client_of.compact
@@ -1155,7 +1155,7 @@ class FinderTest < ActiveRecord::TestCase
def test_with_limiting_with_custom_select
posts = Post.references(:authors).merge(
includes: :author, select: 'posts.*, authors.id as "author_id"',
- limit: 3, order: "posts.id"
+ limit: 3, order: Arel.sql("posts.id")
).to_a
assert_equal 3, posts.size
assert_equal [0, 1, 1], posts.map(&:author_id).sort
diff --git a/activerecord/test/cases/relation/merging_test.rb b/activerecord/test/cases/relation/merging_test.rb
index 953e0fee76..3b35df526f 100644
--- a/activerecord/test/cases/relation/merging_test.rb
+++ b/activerecord/test/cases/relation/merging_test.rb
@@ -13,10 +13,10 @@ class RelationMergingTest < ActiveRecord::TestCase
fixtures :developers, :comments, :authors, :author_addresses, :posts
def test_relation_merging
- devs = Developer.where("salary >= 80000").merge(Developer.limit(2)).merge(Developer.order("id ASC").where("id < 3"))
+ devs = Developer.where("salary >= 80000").merge(Developer.limit(2)).merge(Developer.order(Arel.sql("id ASC")).where("id < 3"))
assert_equal [developers(:david), developers(:jamis)], devs.to_a
- dev_with_count = Developer.limit(1).merge(Developer.order("id DESC")).merge(Developer.select("developers.*"))
+ dev_with_count = Developer.limit(1).merge(Developer.order(Arel.sql("id DESC"))).merge(Developer.select("developers.*"))
assert_equal [developers(:poor_jamis)], dev_with_count.to_a
end
@@ -47,8 +47,8 @@ class RelationMergingTest < ActiveRecord::TestCase
def test_relation_merging_with_eager_load
relations = []
- relations << Post.order("comments.id DESC").merge(Post.eager_load(:last_comment)).merge(Post.all)
- relations << Post.eager_load(:last_comment).merge(Post.order("comments.id DESC")).merge(Post.all)
+ relations << Post.order(Arel.sql("comments.id DESC")).merge(Post.eager_load(:last_comment)).merge(Post.all)
+ relations << Post.eager_load(:last_comment).merge(Post.order(Arel.sql("comments.id DESC"))).merge(Post.all)
relations.each do |posts|
post = posts.find { |p| p.id == 1 }
@@ -57,7 +57,7 @@ class RelationMergingTest < ActiveRecord::TestCase
end
def test_relation_merging_with_locks
- devs = Developer.lock.where("salary >= 80000").order("id DESC").merge(Developer.limit(2))
+ devs = Developer.lock.where("salary >= 80000").order(Arel.sql("id DESC")).merge(Developer.limit(2))
assert devs.locked?
end
@@ -118,7 +118,7 @@ class MergingDifferentRelationsTest < ActiveRecord::TestCase
test "merging where relations" do
hello_by_bob = Post.where(body: "hello").joins(:author).
- merge(Author.where(name: "Bob")).order("posts.id").pluck(Arel.sql("posts.id"))
+ merge(Author.where(name: "Bob")).order(Arel.sql("posts.id")).pluck(Arel.sql("posts.id"))
assert_equal [posts(:misc_by_bob).id,
posts(:other_by_bob).id], hello_by_bob
@@ -131,7 +131,7 @@ class MergingDifferentRelationsTest < ActiveRecord::TestCase
assert_equal ["Bob", "Bob", "David"], posts_by_author_name
posts_by_author_name = Post.limit(3).joins(:author).
- merge(Author.order("name")).pluck(Arel.sql("authors.name"))
+ merge(Author.order(Arel.sql("name"))).pluck(Arel.sql("authors.name"))
assert_equal ["Bob", "Bob", "David"], posts_by_author_name
end
diff --git a/activerecord/test/cases/relation/mutation_test.rb b/activerecord/test/cases/relation/mutation_test.rb
index ad3700b73a..d845c07a50 100644
--- a/activerecord/test/cases/relation/mutation_test.rb
+++ b/activerecord/test/cases/relation/mutation_test.rb
@@ -94,7 +94,7 @@ module ActiveRecord
end
test "reverse_order!" do
- @relation = Post.order("title ASC, comments_count DESC")
+ @relation = Post.order(Arel.sql("title ASC, comments_count DESC"))
relation.reverse_order!
diff --git a/activerecord/test/cases/relation/or_test.rb b/activerecord/test/cases/relation/or_test.rb
index 7e418f9c7d..2abc3b7fe8 100644
--- a/activerecord/test/cases/relation/or_test.rb
+++ b/activerecord/test/cases/relation/or_test.rb
@@ -50,15 +50,15 @@ module ActiveRecord
end
def test_or_preserves_other_querying_methods
- expected = Post.where("id = 1 or id = 2 or id = 3").order("body asc").to_a
- partial = Post.order("body asc")
+ expected = Post.where("id = 1 or id = 2 or id = 3").order(Arel.sql("body asc")).to_a
+ partial = Post.order(Arel.sql("body asc"))
assert_equal expected, partial.where("id = 1").or(partial.where(id: [2, 3])).to_a
- assert_equal expected, Post.order("body asc").where("id = 1").or(Post.order("body asc").where(id: [2, 3])).to_a
+ assert_equal expected, Post.order(Arel.sql("body asc")).where("id = 1").or(Post.order(Arel.sql("body asc")).where(id: [2, 3])).to_a
end
def test_or_with_incompatible_relations
error = assert_raises ArgumentError do
- Post.order("body asc").where("id = 1").or(Post.order("id desc").where(id: [2, 3])).to_a
+ Post.order(Arel.sql("body asc")).where("id = 1").or(Post.order(Arel.sql("id desc")).where(id: [2, 3])).to_a
end
assert_equal "Relation passed to #or must be structurally compatible. Incompatible values: [:order]", error.message
@@ -78,12 +78,12 @@ module ActiveRecord
def test_or_with_unscope_order
expected = Post.where("id = 1 or id = 2")
- assert_equal expected, Post.order("body asc").where("id = 1").unscope(:order).or(Post.where("id = 2")).to_a
+ assert_equal expected, Post.order(Arel.sql("body asc")).where("id = 1").unscope(:order).or(Post.where("id = 2")).to_a
end
def test_or_with_incompatible_unscope
error = assert_raises ArgumentError do
- Post.order("body asc").where("id = 1").or(Post.order("body asc").where("id = 2").unscope(:order)).to_a
+ Post.order(Arel.sql("body asc")).where("id = 1").or(Post.order(Arel.sql("body asc")).where("id = 2").unscope(:order)).to_a
end
assert_equal "Relation passed to #or must be structurally compatible. Incompatible values: [:order]", error.message
@@ -101,7 +101,7 @@ module ActiveRecord
end
def test_or_inside_named_scope
- expected = Post.where("body LIKE '\%a\%' OR title LIKE ?", "%'%").order("id DESC").to_a
+ expected = Post.where("body LIKE '\%a\%' OR title LIKE ?", "%'%").order(Arel.sql("id DESC")).to_a
assert_equal expected, Post.order(id: :desc).typographically_interesting
end
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 72433d1e8e..906f3499dd 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -102,7 +102,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_scoped_first
- topics = Topic.all.order("id ASC")
+ topics = Topic.all.order(Arel.sql("id ASC"))
assert_queries(1) do
2.times { assert_equal "The First Topic", topics.first.title }
@@ -112,7 +112,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_loaded_first
- topics = Topic.all.order("id ASC")
+ topics = Topic.all.order(Arel.sql("id ASC"))
topics.load # force load
assert_no_queries do
@@ -123,7 +123,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_loaded_first_with_limit
- topics = Topic.all.order("id ASC")
+ topics = Topic.all.order(Arel.sql("id ASC"))
topics.load # force load
assert_no_queries do
@@ -135,7 +135,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_first_get_more_than_available
- topics = Topic.all.order("id ASC")
+ topics = Topic.all.order(Arel.sql("id ASC"))
unloaded_first = topics.first(10)
topics.load # force load
@@ -238,7 +238,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_reverse_order_with_function
- topics = Topic.order("length(title)").reverse_order
+ topics = Topic.order(Arel.sql("length(title)")).reverse_order
assert_equal topics(:second).title, topics.first.title
end
@@ -248,24 +248,24 @@ class RelationTest < ActiveRecord::TestCase
end
def test_reverse_order_with_function_other_predicates
- topics = Topic.order("author_name, length(title), id").reverse_order
+ topics = Topic.order(Arel.sql("author_name, length(title), id")).reverse_order
assert_equal topics(:second).title, topics.first.title
- topics = Topic.order("length(author_name), id, length(title)").reverse_order
+ topics = Topic.order(Arel.sql("length(author_name), id, length(title)")).reverse_order
assert_equal topics(:fifth).title, topics.first.title
end
def test_reverse_order_with_multiargument_function
assert_raises(ActiveRecord::IrreversibleOrderError) do
- Topic.order("concat(author_name, title)").reverse_order
+ Topic.order(Arel.sql("concat(author_name, title)")).reverse_order
end
assert_raises(ActiveRecord::IrreversibleOrderError) do
- Topic.order("concat(lower(author_name), title)").reverse_order
+ Topic.order(Arel.sql("concat(lower(author_name), title)")).reverse_order
end
assert_raises(ActiveRecord::IrreversibleOrderError) do
- Topic.order("concat(author_name, lower(title))").reverse_order
+ Topic.order(Arel.sql("concat(author_name, lower(title))")).reverse_order
end
assert_raises(ActiveRecord::IrreversibleOrderError) do
- Topic.order("concat(lower(author_name), title, length(title)").reverse_order
+ Topic.order(Arel.sql("concat(lower(author_name), title, length(title)")).reverse_order
end
end
@@ -277,10 +277,10 @@ class RelationTest < ActiveRecord::TestCase
def test_reverse_order_with_nulls_first_or_last
assert_raises(ActiveRecord::IrreversibleOrderError) do
- Topic.order("title NULLS FIRST").reverse_order
+ Topic.order(Arel.sql("title NULLS FIRST")).reverse_order
end
assert_raises(ActiveRecord::IrreversibleOrderError) do
- Topic.order("title nulls last").reverse_order
+ Topic.order(Arel.sql("title nulls last")).reverse_order
end
end
@@ -319,7 +319,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_raising_exception_on_invalid_hash_params
- e = assert_raise(ArgumentError) { Topic.order(:name, "id DESC", id: :asfsdf) }
+ e = assert_raise(ArgumentError) { Topic.order(Arel.sql("name"), Arel.sql("id DESC"), id: :asfsdf) }
assert_equal 'Direction "asfsdf" is invalid. Valid directions are: [:asc, :desc, :ASC, :DESC, "asc", "desc", "ASC", "DESC"]', e.message
end
@@ -365,7 +365,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_finding_with_order_and_take
- entrants = Entrant.order("id ASC").limit(2).to_a
+ entrants = Entrant.order(Arel.sql("id ASC")).limit(2).to_a
assert_equal 2, entrants.size
assert_equal entrants(:first).name, entrants.first.name
@@ -373,18 +373,21 @@ class RelationTest < ActiveRecord::TestCase
def test_finding_with_cross_table_order_and_limit
tags = Tag.includes(:taggings).
- order("tags.name asc", "taggings.taggable_id asc", "REPLACE('abc', taggings.taggable_type, taggings.taggable_type)").
- limit(1).to_a
+ order(
+ Arel.sql("tags.name asc"),
+ Arel.sql("taggings.taggable_id asc"),
+ Arel.sql("REPLACE('abc', taggings.taggable_type, taggings.taggable_type)")
+ ).limit(1).to_a
assert_equal 1, tags.length
end
def test_finding_with_complex_order_and_limit
- tags = Tag.includes(:taggings).references(:taggings).order("REPLACE('abc', taggings.taggable_type, taggings.taggable_type)").limit(1).to_a
+ tags = Tag.includes(:taggings).references(:taggings).order(Arel.sql("REPLACE('abc', taggings.taggable_type, taggings.taggable_type)")).limit(1).to_a
assert_equal 1, tags.length
end
def test_finding_with_complex_order
- tags = Tag.includes(:taggings).references(:taggings).order("REPLACE('abc', taggings.taggable_type, taggings.taggable_type)").to_a
+ tags = Tag.includes(:taggings).references(:taggings).order(Arel.sql("REPLACE('abc', taggings.taggable_type, taggings.taggable_type)")).to_a
assert_equal 3, tags.length
end
@@ -400,12 +403,12 @@ class RelationTest < ActiveRecord::TestCase
end
def test_finding_with_order_limit_and_offset
- entrants = Entrant.order("id ASC").limit(2).offset(1)
+ entrants = Entrant.order(Arel.sql("id ASC")).limit(2).offset(1)
assert_equal 2, entrants.to_a.size
assert_equal entrants(:second).name, entrants.first.name
- entrants = Entrant.order("id ASC").limit(2).offset(2)
+ entrants = Entrant.order(Arel.sql("id ASC")).limit(2).offset(2)
assert_equal 1, entrants.to_a.size
assert_equal entrants(:third).name, entrants.first.name
end
@@ -504,7 +507,7 @@ class RelationTest < ActiveRecord::TestCase
def test_eager_association_loading_of_stis_with_multiple_references
authors = Author.eager_load(posts: { special_comments: { post: [ :special_comments, :very_special_comment ] } }).
- order("comments.body, very_special_comments_posts.body").where("posts.id = 4").to_a
+ order(Arel.sql("comments.body, very_special_comments_posts.body")).where("posts.id = 4").to_a
assert_equal [authors(:david)], authors
assert_no_queries do
@@ -515,27 +518,27 @@ class RelationTest < ActiveRecord::TestCase
def test_find_with_preloaded_associations
assert_queries(2) do
- posts = Post.preload(:comments).order("posts.id")
+ posts = Post.preload(:comments).order(Arel.sql("posts.id"))
assert posts.first.comments.first
end
assert_queries(2) do
- posts = Post.preload(:comments).order("posts.id")
+ posts = Post.preload(:comments).order(Arel.sql("posts.id"))
assert posts.first.comments.first
end
assert_queries(2) do
- posts = Post.preload(:author).order("posts.id")
+ posts = Post.preload(:author).order(Arel.sql("posts.id"))
assert posts.first.author
end
assert_queries(2) do
- posts = Post.preload(:author).order("posts.id")
+ posts = Post.preload(:author).order(Arel.sql("posts.id"))
assert posts.first.author
end
assert_queries(3) do
- posts = Post.preload(:author, :comments).order("posts.id")
+ posts = Post.preload(:author, :comments).order(Arel.sql("posts.id"))
assert posts.first.author
assert posts.first.comments.first
end
@@ -550,22 +553,22 @@ class RelationTest < ActiveRecord::TestCase
def test_find_with_included_associations
assert_queries(2) do
- posts = Post.includes(:comments).order("posts.id")
+ posts = Post.includes(:comments).order(Arel.sql("posts.id"))
assert posts.first.comments.first
end
assert_queries(2) do
- posts = Post.all.includes(:comments).order("posts.id")
+ posts = Post.all.includes(:comments).order(Arel.sql("posts.id"))
assert posts.first.comments.first
end
assert_queries(2) do
- posts = Post.includes(:author).order("posts.id")
+ posts = Post.includes(:author).order(Arel.sql("posts.id"))
assert posts.first.author
end
assert_queries(3) do
- posts = Post.includes(:author, :comments).order("posts.id")
+ posts = Post.includes(:author, :comments).order(Arel.sql("posts.id"))
assert posts.first.author
assert posts.first.comments.first
end
@@ -577,7 +580,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_includes_with_select
- query = Post.select("comments_count AS ranking").order("ranking").includes(:comments)
+ query = Post.select("comments_count AS ranking").order(Arel.sql("ranking")).includes(:comments)
.where(comments: { id: 1 })
assert_equal ["comments_count AS ranking"], query.select_values
@@ -646,9 +649,9 @@ class RelationTest < ActiveRecord::TestCase
def test_to_sql_on_eager_join
expected = assert_sql {
- Post.eager_load(:last_comment).order("comments.id DESC").to_a
+ Post.eager_load(:last_comment).order(Arel.sql("comments.id DESC")).to_a
}.first
- actual = Post.eager_load(:last_comment).order("comments.id DESC").to_sql
+ actual = Post.eager_load(:last_comment).order(Arel.sql("comments.id DESC")).to_sql
assert_equal expected, actual
end
@@ -659,7 +662,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_loading_with_one_association_with_non_preload
- posts = Post.eager_load(:last_comment).order("comments.id DESC")
+ posts = Post.eager_load(:last_comment).order(Arel.sql("comments.id DESC"))
post = posts.find { |p| p.id == 1 }
assert_equal Post.find(1).last_comment, post.last_comment
end
@@ -695,7 +698,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_find_ids
- authors = Author.order("id ASC")
+ authors = Author.order(Arel.sql("id ASC"))
results = authors.find(authors(:david).id, authors(:mary).id)
assert_kind_of Array, results
@@ -979,7 +982,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_multiple_selects
- post = Post.all.select("comments_count").select("title").order("id ASC").first
+ post = Post.all.select("comments_count").select("title").order(Arel.sql("id ASC")).first
assert_equal "Welcome to the weblog", post.title
assert_equal 2, post.comments_count
end
@@ -1341,7 +1344,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_except
- relation = Post.where(author_id: 1).order("id ASC").limit(1)
+ relation = Post.where(author_id: 1).order(Arel.sql("id ASC")).limit(1)
assert_equal [posts(:welcome)], relation.to_a
author_posts = relation.except(:order, :limit)
@@ -1352,7 +1355,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_only
- relation = Post.where(author_id: 1).order("id ASC").limit(1)
+ relation = Post.where(author_id: 1).order(Arel.sql("id ASC")).limit(1)
assert_equal [posts(:welcome)], relation.to_a
author_posts = relation.only(:where)
@@ -1363,7 +1366,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_anonymous_extension
- relation = Post.where(author_id: 1).order("id ASC").extending do
+ relation = Post.where(author_id: 1).order(Arel.sql("id ASC")).extending do
def author
"lifo"
end
@@ -1374,7 +1377,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_named_extension
- relation = Post.where(author_id: 1).order("id ASC").extending(Post::NamedExtension)
+ relation = Post.where(author_id: 1).order(Arel.sql("id ASC")).extending(Post::NamedExtension)
assert_equal "lifo", relation.author
assert_equal "lifo", relation.limit(1).author
end
@@ -1389,13 +1392,13 @@ class RelationTest < ActiveRecord::TestCase
end
def test_order_using_scoping
- car1 = CoolCar.order("id DESC").scoping do
- CoolCar.all.merge!(order: "id asc").first
+ car1 = CoolCar.order(Arel.sql("id DESC")).scoping do
+ CoolCar.all.merge!(order: Arel.sql("id asc")).first
end
assert_equal "zyke", car1.name
- car2 = FastCar.order("id DESC").scoping do
- FastCar.all.merge!(order: "id asc").first
+ car2 = FastCar.order(Arel.sql("id DESC")).scoping do
+ FastCar.all.merge!(order: Arel.sql("id asc")).first
end
assert_equal "zyke", car2.name
end
@@ -1418,7 +1421,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_ordering_with_extra_spaces
- assert_equal authors(:david), Author.order("id DESC , name DESC").last
+ assert_equal authors(:david), Author.order(Arel.sql("id DESC , name DESC")).last
end
def test_update_all_with_blank_argument
@@ -1439,7 +1442,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_update_all_with_joins_and_limit_and_order
- comments = Comment.joins(:post).where("posts.id" => posts(:welcome).id).order("comments.id").limit(1)
+ comments = Comment.joins(:post).where("posts.id" => posts(:welcome).id).order(Arel.sql("comments.id")).limit(1)
assert_equal 1, comments.update_all(post_id: posts(:thinking).id)
assert_equal posts(:thinking), comments(:greetings).post
assert_equal posts(:welcome), comments(:more_greetings).post
@@ -1454,7 +1457,7 @@ class RelationTest < ActiveRecord::TestCase
end
def test_update_all_with_joins_and_offset_and_order
- all_comments = Comment.joins(:post).where("posts.id" => posts(:welcome).id).order("posts.id", "comments.id")
+ all_comments = Comment.joins(:post).where("posts.id" => posts(:welcome).id).order(Arel.sql("posts.id"), Arel.sql("comments.id"))
count = all_comments.count
comments = all_comments.offset(1)
@@ -1564,52 +1567,52 @@ class RelationTest < ActiveRecord::TestCase
end
def test_automatically_added_order_references
- scope = Post.order("comments.body")
+ scope = Post.order(Arel.sql("comments.body"))
assert_equal ["comments"], scope.references_values
- scope = Post.order("#{Comment.quoted_table_name}.#{Comment.quoted_primary_key}")
+ scope = Post.order(Arel.sql("#{Comment.quoted_table_name}.#{Comment.quoted_primary_key}"))
if current_adapter?(:OracleAdapter)
assert_equal ["COMMENTS"], scope.references_values
else
assert_equal ["comments"], scope.references_values
end
- scope = Post.order("comments.body", "yaks.body")
+ scope = Post.order(Arel.sql("comments.body"), Arel.sql("yaks.body"))
assert_equal ["comments", "yaks"], scope.references_values
# Don't infer yaks, let's not go down that road again...
- scope = Post.order("comments.body, yaks.body")
+ scope = Post.order(Arel.sql("comments.body, yaks.body"))
assert_equal ["comments"], scope.references_values
- scope = Post.order("comments.body asc")
+ scope = Post.order(Arel.sql("comments.body asc"))
assert_equal ["comments"], scope.references_values
- scope = Post.order("foo(comments.body)")
+ scope = Post.order(Arel.sql("foo(comments.body)"))
assert_equal [], scope.references_values
end
def test_automatically_added_reorder_references
- scope = Post.reorder("comments.body")
+ scope = Post.reorder(Arel.sql("comments.body"))
assert_equal %w(comments), scope.references_values
- scope = Post.reorder("#{Comment.quoted_table_name}.#{Comment.quoted_primary_key}")
+ scope = Post.reorder(Arel.sql("#{Comment.quoted_table_name}.#{Comment.quoted_primary_key}"))
if current_adapter?(:OracleAdapter)
assert_equal ["COMMENTS"], scope.references_values
else
assert_equal ["comments"], scope.references_values
end
- scope = Post.reorder("comments.body", "yaks.body")
+ scope = Post.reorder(Arel.sql("comments.body"), Arel.sql("yaks.body"))
assert_equal %w(comments yaks), scope.references_values
# Don't infer yaks, let's not go down that road again...
- scope = Post.reorder("comments.body, yaks.body")
+ scope = Post.reorder(Arel.sql("comments.body, yaks.body"))
assert_equal %w(comments), scope.references_values
- scope = Post.reorder("comments.body asc")
+ scope = Post.reorder(Arel.sql("comments.body asc"))
assert_equal %w(comments), scope.references_values
- scope = Post.reorder("foo(comments.body)")
+ scope = Post.reorder(Arel.sql("foo(comments.body)"))
assert_equal [], scope.references_values
end
@@ -1811,7 +1814,7 @@ class RelationTest < ActiveRecord::TestCase
end
test "joins with select" do
- posts = Post.joins(:author).select("id", "authors.author_address_id").order("posts.id").limit(3)
+ posts = Post.joins(:author).select("id", "authors.author_address_id").order(Arel.sql("posts.id")).limit(3)
assert_equal [1, 2, 4], posts.map(&:id)
assert_equal [1, 1, 1], posts.map(&:author_address_id)
end
diff --git a/activerecord/test/cases/scoping/default_scoping_test.rb b/activerecord/test/cases/scoping/default_scoping_test.rb
index 716ca29eda..26f0b86703 100644
--- a/activerecord/test/cases/scoping/default_scoping_test.rb
+++ b/activerecord/test/cases/scoping/default_scoping_test.rb
@@ -13,7 +13,7 @@ class DefaultScopingTest < ActiveRecord::TestCase
fixtures :developers, :posts, :comments
def test_default_scope
- expected = Developer.all.merge!(order: "salary DESC").to_a.collect(&:salary)
+ expected = Developer.all.merge!(order: Arel.sql("salary DESC")).to_a.collect(&:salary)
received = DeveloperOrderedBySalary.all.collect(&:salary)
assert_equal expected, received
end
@@ -80,20 +80,20 @@ class DefaultScopingTest < ActiveRecord::TestCase
end
def test_scope_overwrites_default
- expected = Developer.all.merge!(order: "salary DESC, name DESC").to_a.collect(&:name)
+ expected = Developer.all.merge!(order: Arel.sql("salary DESC, name DESC")).to_a.collect(&:name)
received = DeveloperOrderedBySalary.by_name.to_a.collect(&:name)
assert_equal expected, received
end
def test_reorder_overrides_default_scope_order
- expected = Developer.order("name DESC").collect(&:name)
- received = DeveloperOrderedBySalary.reorder("name DESC").collect(&:name)
+ expected = Developer.order(Arel.sql("name DESC")).collect(&:name)
+ received = DeveloperOrderedBySalary.reorder(Arel.sql("name DESC")).collect(&:name)
assert_equal expected, received
end
def test_order_after_reorder_combines_orders
- expected = Developer.order("name DESC, id DESC").collect { |dev| [dev.name, dev.id] }
- received = Developer.order("name ASC").reorder("name DESC").order("id DESC").collect { |dev| [dev.name, dev.id] }
+ expected = Developer.order(Arel.sql("name DESC, id DESC")).collect { |dev| [dev.name, dev.id] }
+ received = Developer.order(Arel.sql("name ASC")).reorder(Arel.sql("name DESC")).order(Arel.sql("id DESC")).collect { |dev| [dev.name, dev.id] }
assert_equal expected, received
end
@@ -104,69 +104,69 @@ class DefaultScopingTest < ActiveRecord::TestCase
end
def test_unscope_after_reordering_and_combining
- expected = Developer.order("id DESC, name DESC").collect { |dev| [dev.name, dev.id] }
- received = DeveloperOrderedBySalary.reorder("name DESC").unscope(:order).order("id DESC, name DESC").collect { |dev| [dev.name, dev.id] }
+ expected = Developer.order(Arel.sql("id DESC, name DESC")).collect { |dev| [dev.name, dev.id] }
+ received = DeveloperOrderedBySalary.reorder(Arel.sql("name DESC")).unscope(:order).order(Arel.sql("id DESC, name DESC")).collect { |dev| [dev.name, dev.id] }
assert_equal expected, received
expected_2 = Developer.all.collect { |dev| [dev.name, dev.id] }
- received_2 = Developer.order("id DESC, name DESC").unscope(:order).collect { |dev| [dev.name, dev.id] }
+ received_2 = Developer.order(Arel.sql("id DESC, name DESC")).unscope(:order).collect { |dev| [dev.name, dev.id] }
assert_equal expected_2, received_2
expected_3 = Developer.all.collect { |dev| [dev.name, dev.id] }
- received_3 = Developer.reorder("name DESC").unscope(:order).collect { |dev| [dev.name, dev.id] }
+ received_3 = Developer.reorder(Arel.sql("name DESC")).unscope(:order).collect { |dev| [dev.name, dev.id] }
assert_equal expected_3, received_3
end
def test_unscope_with_where_attributes
- expected = Developer.order("salary DESC").collect(&:name)
+ expected = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received = DeveloperOrderedBySalary.where(name: "David").unscope(where: :name).collect(&:name)
assert_equal expected, received
- expected_2 = Developer.order("salary DESC").collect(&:name)
+ expected_2 = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received_2 = DeveloperOrderedBySalary.select("id").where("name" => "Jamis").unscope({ where: :name }, :select).collect(&:name)
assert_equal expected_2, received_2
- expected_3 = Developer.order("salary DESC").collect(&:name)
+ expected_3 = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received_3 = DeveloperOrderedBySalary.select("id").where("name" => "Jamis").unscope(:select, :where).collect(&:name)
assert_equal expected_3, received_3
- expected_4 = Developer.order("salary DESC").collect(&:name)
+ expected_4 = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received_4 = DeveloperOrderedBySalary.where.not("name" => "Jamis").unscope(where: :name).collect(&:name)
assert_equal expected_4, received_4
- expected_5 = Developer.order("salary DESC").collect(&:name)
+ expected_5 = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received_5 = DeveloperOrderedBySalary.where.not("name" => ["Jamis", "David"]).unscope(where: :name).collect(&:name)
assert_equal expected_5, received_5
- expected_6 = Developer.order("salary DESC").collect(&:name)
+ expected_6 = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received_6 = DeveloperOrderedBySalary.where(Developer.arel_table["name"].eq("David")).unscope(where: :name).collect(&:name)
assert_equal expected_6, received_6
- expected_7 = Developer.order("salary DESC").collect(&:name)
+ expected_7 = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received_7 = DeveloperOrderedBySalary.where(Developer.arel_table[:name].eq("David")).unscope(where: :name).collect(&:name)
assert_equal expected_7, received_7
end
def test_unscope_comparison_where_clauses
# unscoped for WHERE (`developers`.`id` <= 2)
- expected = Developer.order("salary DESC").collect(&:name)
+ expected = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received = DeveloperOrderedBySalary.where(id: -Float::INFINITY..2).unscope(where: :id).collect { |dev| dev.name }
assert_equal expected, received
# unscoped for WHERE (`developers`.`id` < 2)
- expected = Developer.order("salary DESC").collect(&:name)
+ expected = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received = DeveloperOrderedBySalary.where(id: -Float::INFINITY...2).unscope(where: :id).collect { |dev| dev.name }
assert_equal expected, received
end
def test_unscope_multiple_where_clauses
- expected = Developer.order("salary DESC").collect(&:name)
+ expected = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received = DeveloperOrderedBySalary.where(name: "Jamis").where(id: 1).unscope(where: [:name, :id]).collect(&:name)
assert_equal expected, received
end
def test_unscope_string_where_clauses_involved
- dev_relation = Developer.order("salary DESC").where("created_at > ?", 1.year.ago)
+ dev_relation = Developer.order(Arel.sql("salary DESC")).where("created_at > ?", 1.year.ago)
expected = dev_relation.collect(&:name)
dev_ordered_relation = DeveloperOrderedBySalary.where(name: "Jamis").where("created_at > ?", 1.year.ago)
@@ -176,35 +176,35 @@ class DefaultScopingTest < ActiveRecord::TestCase
end
def test_unscope_with_grouping_attributes
- expected = Developer.order("salary DESC").collect(&:name)
+ expected = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received = DeveloperOrderedBySalary.group(:name).unscope(:group).collect(&:name)
assert_equal expected, received
- expected_2 = Developer.order("salary DESC").collect(&:name)
+ expected_2 = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received_2 = DeveloperOrderedBySalary.group("name").unscope(:group).collect(&:name)
assert_equal expected_2, received_2
end
def test_unscope_with_limit_in_query
- expected = Developer.order("salary DESC").collect(&:name)
+ expected = Developer.order(Arel.sql("salary DESC")).collect(&:name)
received = DeveloperOrderedBySalary.limit(1).unscope(:limit).collect(&:name)
assert_equal expected, received
end
def test_order_to_unscope_reordering
- scope = DeveloperOrderedBySalary.order("salary DESC, name ASC").reverse_order.unscope(:order)
+ scope = DeveloperOrderedBySalary.order(Arel.sql("salary DESC, name ASC")).reverse_order.unscope(:order)
assert !/order/i.match?(scope.to_sql)
end
def test_unscope_reverse_order
expected = Developer.all.collect(&:name)
- received = Developer.order("salary DESC").reverse_order.unscope(:order).collect(&:name)
+ received = Developer.order(Arel.sql("salary DESC")).reverse_order.unscope(:order).collect(&:name)
assert_equal expected, received
end
def test_unscope_select
- expected = Developer.order("salary ASC").collect(&:name)
- received = Developer.order("salary DESC").reverse_order.select(:name).unscope(:select).collect(&:name)
+ expected = Developer.order(Arel.sql("salary ASC")).collect(&:name)
+ received = Developer.order(Arel.sql("salary DESC")).reverse_order.select(:name).unscope(:select).collect(&:name)
assert_equal expected, received
expected_2 = Developer.all.collect(&:id)
@@ -256,11 +256,11 @@ class DefaultScopingTest < ActiveRecord::TestCase
end
assert_raises(ArgumentError) do
- Developer.order("name DESC").reverse_order.unscope(:reverse_order)
+ Developer.order(Arel.sql("name DESC")).reverse_order.unscope(:reverse_order)
end
assert_raises(ArgumentError) do
- Developer.order("name DESC").where(name: "Jamis").unscope()
+ Developer.order(Arel.sql("name DESC")).where(name: "Jamis").unscope()
end
end
@@ -295,7 +295,7 @@ class DefaultScopingTest < ActiveRecord::TestCase
end
def test_order_in_default_scope_should_not_prevail
- expected = Developer.all.merge!(order: "salary desc").to_a.collect(&:salary)
+ expected = Developer.all.merge!(order: Arel.sql("salary desc")).to_a.collect(&:salary)
received = DeveloperOrderedBySalary.all.merge!(order: "salary").to_a.collect(&:salary)
assert_equal expected, received
end
diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb
index b0431a4e34..4079bb477e 100644
--- a/activerecord/test/cases/scoping/named_scoping_test.rb
+++ b/activerecord/test/cases/scoping/named_scoping_test.rb
@@ -472,7 +472,7 @@ class NamedScopingTest < ActiveRecord::TestCase
def test_scopes_on_relations
# Topic.replied
- approved_topics = Topic.all.approved.order("id DESC")
+ approved_topics = Topic.all.approved.order(Arel.sql("id DESC"))
assert_equal topics(:fifth), approved_topics.first
replied_approved_topics = approved_topics.replied
@@ -480,7 +480,7 @@ class NamedScopingTest < ActiveRecord::TestCase
end
def test_index_on_scope
- approved = Topic.approved.order("id ASC")
+ approved = Topic.approved.order(Arel.sql("id ASC"))
assert_equal topics(:second), approved[0]
assert approved.loaded?
end
diff --git a/activerecord/test/cases/scoping/relation_scoping_test.rb b/activerecord/test/cases/scoping/relation_scoping_test.rb
index 116f8e83aa..2a95204d0d 100644
--- a/activerecord/test/cases/scoping/relation_scoping_test.rb
+++ b/activerecord/test/cases/scoping/relation_scoping_test.rb
@@ -39,23 +39,23 @@ class RelationScopingTest < ActiveRecord::TestCase
end
def test_reverse_order
- assert_equal Developer.order("id DESC").to_a.reverse, Developer.order("id DESC").reverse_order
+ assert_equal Developer.order(Arel.sql("id DESC")).to_a.reverse, Developer.order(Arel.sql("id DESC")).reverse_order
end
def test_reverse_order_with_arel_node
- assert_equal Developer.order("id DESC").to_a.reverse, Developer.order(Developer.arel_table[:id].desc).reverse_order
+ assert_equal Developer.order(Arel.sql("id DESC")).to_a.reverse, Developer.order(Developer.arel_table[:id].desc).reverse_order
end
def test_reverse_order_with_multiple_arel_nodes
- assert_equal Developer.order("id DESC").order("name DESC").to_a.reverse, Developer.order(Developer.arel_table[:id].desc).order(Developer.arel_table[:name].desc).reverse_order
+ assert_equal Developer.order(Arel.sql("id DESC")).order(Arel.sql("name DESC")).to_a.reverse, Developer.order(Developer.arel_table[:id].desc).order(Developer.arel_table[:name].desc).reverse_order
end
def test_reverse_order_with_arel_nodes_and_strings
- assert_equal Developer.order("id DESC").order("name DESC").to_a.reverse, Developer.order("id DESC").order(Developer.arel_table[:name].desc).reverse_order
+ assert_equal Developer.order(Arel.sql("id DESC")).order(Arel.sql("name DESC")).to_a.reverse, Developer.order(Arel.sql("id DESC")).order(Developer.arel_table[:name].desc).reverse_order
end
def test_double_reverse_order_produces_original_order
- assert_equal Developer.order("name DESC"), Developer.order("name DESC").reverse_order.reverse_order
+ assert_equal Developer.order(Arel.sql("name DESC")), Developer.order(Arel.sql("name DESC")).reverse_order.reverse_order
end
def test_scoped_find
@@ -72,7 +72,7 @@ class RelationScopingTest < ActiveRecord::TestCase
end
def test_scoped_find_last
- highest_salary = Developer.order("salary DESC").first
+ highest_salary = Developer.order(Arel.sql("salary DESC")).first
Developer.order("salary").scoping do
assert_equal highest_salary, Developer.last
@@ -80,8 +80,8 @@ class RelationScopingTest < ActiveRecord::TestCase
end
def test_scoped_find_last_preserves_scope
- lowest_salary = Developer.order("salary ASC").first
- highest_salary = Developer.order("salary DESC").first
+ lowest_salary = Developer.order(Arel.sql("salary ASC")).first
+ highest_salary = Developer.order(Arel.sql("salary DESC")).first
Developer.order("salary").scoping do
assert_equal highest_salary, Developer.last
diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb
index cb8686f315..11fe073ab0 100644
--- a/activerecord/test/models/author.rb
+++ b/activerecord/test/models/author.rb
@@ -7,10 +7,10 @@ class Author < ActiveRecord::Base
has_many :very_special_comments, through: :posts
has_many :posts_with_comments, -> { includes(:comments) }, class_name: "Post"
has_many :popular_grouped_posts, -> { includes(:comments).group("type").having("SUM(comments_count) > 1").select("type") }, class_name: "Post"
- has_many :posts_with_comments_sorted_by_comment_id, -> { includes(:comments).order("comments.id") }, class_name: "Post"
- has_many :posts_sorted_by_id_limited, -> { order("posts.id").limit(1) }, class_name: "Post"
+ has_many :posts_with_comments_sorted_by_comment_id, -> { includes(:comments).order(Arel.sql("comments.id")) }, class_name: "Post"
+ has_many :posts_sorted_by_id_limited, -> { order(Arel.sql("posts.id")).limit(1) }, class_name: "Post"
has_many :posts_with_categories, -> { includes(:categories) }, class_name: "Post"
- has_many :posts_with_comments_and_categories, -> { includes(:comments, :categories).order("posts.id") }, class_name: "Post"
+ has_many :posts_with_comments_and_categories, -> { includes(:comments, :categories).order(Arel.sql("posts.id")) }, class_name: "Post"
has_many :posts_with_special_categorizations, class_name: "PostWithSpecialCategorization"
has_one :post_about_thinking, -> { where("posts.title like '%thinking%'") }, class_name: "Post"
has_one :post_about_thinking_with_last_comment, -> { where("posts.title like '%thinking%'").includes(:last_comment) }, class_name: "Post"
@@ -20,15 +20,15 @@ class Author < ActiveRecord::Base
end
end
has_many :comments_containing_the_letter_e, through: :posts, source: :comments
- has_many :comments_with_order_and_conditions, -> { order("comments.body").where("comments.body like 'Thank%'") }, through: :posts, source: :comments
+ has_many :comments_with_order_and_conditions, -> { order(Arel.sql("comments.body")).where("comments.body like 'Thank%'") }, through: :posts, source: :comments
has_many :comments_with_include, -> { includes(:post).where(posts: { type: "Post" }) }, through: :posts, source: :comments
has_many :comments_for_first_author, -> { for_first_author }, through: :posts, source: :comments
has_many :first_posts
- has_many :comments_on_first_posts, -> { order("posts.id desc, comments.id asc") }, through: :first_posts, source: :comments
+ has_many :comments_on_first_posts, -> { order(Arel.sql("posts.id desc, comments.id asc")) }, through: :first_posts, source: :comments
has_one :first_post
- has_one :comment_on_first_post, -> { order("posts.id desc, comments.id asc") }, through: :first_post, source: :comments
+ has_one :comment_on_first_post, -> { order(Arel.sql("posts.id desc, comments.id asc")) }, through: :first_post, source: :comments
has_many :thinking_posts, -> { where(title: "So I was thinking") }, dependent: :delete_all, class_name: "Post"
has_many :welcome_posts, -> { where(title: "Welcome to the weblog") }, class_name: "Post"
@@ -40,11 +40,11 @@ class Author < ActiveRecord::Base
-> { where(title: "Welcome to the weblog").where(Post.arel_table[:comments_count].gt(0)) },
class_name: "Post"
- has_many :comments_desc, -> { order("comments.id DESC") }, through: :posts, source: :comments
+ has_many :comments_desc, -> { order(Arel.sql("comments.id DESC")) }, through: :posts, source: :comments
has_many :unordered_comments, -> { unscope(:order).distinct }, through: :posts_sorted_by_id_limited, source: :comments
has_many :funky_comments, through: :posts, source: :comments
- has_many :ordered_uniq_comments, -> { distinct.order("comments.id") }, through: :posts, source: :comments
- has_many :ordered_uniq_comments_desc, -> { distinct.order("comments.id DESC") }, through: :posts, source: :comments
+ has_many :ordered_uniq_comments, -> { distinct.order(Arel.sql("comments.id")) }, through: :posts, source: :comments
+ has_many :ordered_uniq_comments_desc, -> { distinct.order(Arel.sql("comments.id DESC")) }, through: :posts, source: :comments
has_many :readonly_comments, -> { readonly }, through: :posts, source: :comments
has_many :special_posts
@@ -107,15 +107,15 @@ class Author < ActiveRecord::Base
has_many :similar_posts, -> { distinct }, through: :tags, source: :tagged_posts
has_many :ordered_posts, -> { distinct }, through: :ordered_tags, source: :tagged_posts
- has_many :distinct_tags, -> { select("DISTINCT tags.*").order("tags.name") }, through: :posts, source: :tags
+ has_many :distinct_tags, -> { select("DISTINCT tags.*").order(Arel.sql("tags.name")) }, through: :posts, source: :tags
has_many :tags_with_primary_key, through: :posts
has_many :books
has_many :unpublished_books, -> { where(status: [:proposed, :written]) }, class_name: "Book"
has_many :subscriptions, through: :books
- has_many :subscribers, -> { order("subscribers.nick") }, through: :subscriptions
- has_many :distinct_subscribers, -> { select("DISTINCT subscribers.*").order("subscribers.nick") }, through: :subscriptions, source: :subscriber
+ has_many :subscribers, -> { order(Arel.sql("subscribers.nick")) }, through: :subscriptions
+ has_many :distinct_subscribers, -> { select("DISTINCT subscribers.*").order(Arel.sql("subscribers.nick")) }, through: :subscriptions, source: :subscriber
has_one :essay, primary_key: :name, as: :writer
has_one :essay_category, through: :essay, source: :category
diff --git a/activerecord/test/models/car.rb b/activerecord/test/models/car.rb
index 3d6a7a96c2..0be943a321 100644
--- a/activerecord/test/models/car.rb
+++ b/activerecord/test/models/car.rb
@@ -19,13 +19,13 @@ class Car < ActiveRecord::Base
scope :incl_tyres, -> { includes(:tyres) }
scope :incl_engines, -> { includes(:engines) }
- scope :order_using_new_style, -> { order("name asc") }
+ scope :order_using_new_style, -> { order(Arel.sql("name asc")) }
end
class CoolCar < Car
- default_scope { order("name desc") }
+ default_scope { order(Arel.sql("name desc")) }
end
class FastCar < Car
- default_scope { order("name desc") }
+ default_scope { order(Arel.sql("name desc")) }
end
diff --git a/activerecord/test/models/category.rb b/activerecord/test/models/category.rb
index 2ccc00bed9..3038264694 100644
--- a/activerecord/test/models/category.rb
+++ b/activerecord/test/models/category.rb
@@ -4,7 +4,7 @@ class Category < ActiveRecord::Base
has_and_belongs_to_many :posts
has_and_belongs_to_many :special_posts, class_name: "Post"
has_and_belongs_to_many :other_posts, class_name: "Post"
- has_and_belongs_to_many :posts_with_authors_sorted_by_author_id, -> { includes(:authors).order("authors.id") }, class_name: "Post"
+ has_and_belongs_to_many :posts_with_authors_sorted_by_author_id, -> { includes(:authors).order(Arel.sql("authors.id")) }, class_name: "Post"
has_and_belongs_to_many :select_testing_posts,
-> { select "posts.*, 1 as correctness_marker" },
diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb
index 5ab433f2d9..d5acfc0749 100644
--- a/activerecord/test/models/comment.rb
+++ b/activerecord/test/models/comment.rb
@@ -81,7 +81,7 @@ class CommentThatAutomaticallyAltersPostBody < Comment
end
class CommentWithDefaultScopeReferencesAssociation < Comment
- default_scope -> { includes(:developer).order("developers.name").references(:developer) }
+ default_scope -> { includes(:developer).order(Arel.sql("developers.name")).references(:developer) }
belongs_to :developer
end
diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb
index bbc5fc2b2d..1a82a9e646 100644
--- a/activerecord/test/models/company.rb
+++ b/activerecord/test/models/company.rb
@@ -50,7 +50,7 @@ class Firm < Company
has_many :clients, -> { order "id" }, dependent: :destroy, before_remove: :log_before_remove, after_remove: :log_after_remove
has_many :unsorted_clients, class_name: "Client"
has_many :unsorted_clients_with_symbol, class_name: :Client
- has_many :clients_sorted_desc, -> { order "id DESC" }, class_name: "Client"
+ has_many :clients_sorted_desc, -> { order Arel.sql("id DESC") }, class_name: "Client"
has_many :clients_of_firm, -> { order "id" }, foreign_key: "client_of", class_name: "Client", inverse_of: :firm
has_many :clients_ordered_by_name, -> { order "name" }, class_name: "Client"
has_many :unvalidated_clients_of_firm, foreign_key: "client_of", class_name: "Client", validate: false
diff --git a/activerecord/test/models/company_in_module.rb b/activerecord/test/models/company_in_module.rb
index 52b7e06a63..9108eb5249 100644
--- a/activerecord/test/models/company_in_module.rb
+++ b/activerecord/test/models/company_in_module.rb
@@ -9,7 +9,7 @@ module MyApplication
class Firm < Company
has_many :clients, -> { order("id") }, dependent: :destroy
- has_many :clients_sorted_desc, -> { order("id DESC") }, class_name: "Client"
+ has_many :clients_sorted_desc, -> { order(Arel.sql("id DESC")) }, class_name: "Client"
has_many :clients_of_firm, -> { order "id" }, foreign_key: "client_of", class_name: "Client"
has_many :clients_like_ms, -> { where("name = 'Microsoft'").order("id") }, class_name: "Client"
has_one :account, class_name: "MyApplication::Billing::Account", dependent: :destroy
diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb
index 8881c69368..c2a21eac04 100644
--- a/activerecord/test/models/developer.rb
+++ b/activerecord/test/models/developer.rb
@@ -4,7 +4,7 @@ require "ostruct"
module DeveloperProjectsAssociationExtension2
def find_least_recent
- order("id ASC").first
+ order(Arel.sql("id ASC")).first
end
end
@@ -13,7 +13,7 @@ class Developer < ActiveRecord::Base
has_and_belongs_to_many :projects do
def find_most_recent
- order("id DESC").first
+ order(Arel.sql("id DESC")).first
end
end
@@ -41,7 +41,7 @@ class Developer < ActiveRecord::Base
join_table: "developers_projects",
association_foreign_key: "project_id" do
def find_least_recent
- order("id ASC").first
+ order(Arel.sql("id ASC")).first
end
end
@@ -126,7 +126,7 @@ end
class DeveloperFilteredOnJoins < ActiveRecord::Base
self.table_name = "developers"
- has_and_belongs_to_many :projects, -> { order("projects.id") }, foreign_key: "developer_id", join_table: "developers_projects"
+ has_and_belongs_to_many :projects, -> { order(Arel.sql("projects.id")) }, foreign_key: "developer_id", join_table: "developers_projects"
def self.default_scope
joins(:projects).where(projects: { name: "Active Controller" })
@@ -135,9 +135,9 @@ end
class DeveloperOrderedBySalary < ActiveRecord::Base
self.table_name = "developers"
- default_scope { order("salary DESC") }
+ default_scope { order(Arel.sql("salary DESC")) }
- scope :by_name, -> { order("name DESC") }
+ scope :by_name, -> { order(Arel.sql("name DESC")) }
end
class DeveloperCalledDavid < ActiveRecord::Base
@@ -225,14 +225,14 @@ end
class EagerDeveloperWithDefaultScope < ActiveRecord::Base
self.table_name = "developers"
- has_and_belongs_to_many :projects, -> { order("projects.id") }, foreign_key: "developer_id", join_table: "developers_projects"
+ has_and_belongs_to_many :projects, -> { order(Arel.sql("projects.id")) }, foreign_key: "developer_id", join_table: "developers_projects"
default_scope { includes(:projects) }
end
class EagerDeveloperWithClassMethodDefaultScope < ActiveRecord::Base
self.table_name = "developers"
- has_and_belongs_to_many :projects, -> { order("projects.id") }, foreign_key: "developer_id", join_table: "developers_projects"
+ has_and_belongs_to_many :projects, -> { order(Arel.sql("projects.id")) }, foreign_key: "developer_id", join_table: "developers_projects"
def self.default_scope
includes(:projects)
@@ -241,21 +241,21 @@ end
class EagerDeveloperWithLambdaDefaultScope < ActiveRecord::Base
self.table_name = "developers"
- has_and_belongs_to_many :projects, -> { order("projects.id") }, foreign_key: "developer_id", join_table: "developers_projects"
+ has_and_belongs_to_many :projects, -> { order(Arel.sql("projects.id")) }, foreign_key: "developer_id", join_table: "developers_projects"
default_scope lambda { includes(:projects) }
end
class EagerDeveloperWithBlockDefaultScope < ActiveRecord::Base
self.table_name = "developers"
- has_and_belongs_to_many :projects, -> { order("projects.id") }, foreign_key: "developer_id", join_table: "developers_projects"
+ has_and_belongs_to_many :projects, -> { order(Arel.sql("projects.id")) }, foreign_key: "developer_id", join_table: "developers_projects"
default_scope { includes(:projects) }
end
class EagerDeveloperWithCallableDefaultScope < ActiveRecord::Base
self.table_name = "developers"
- has_and_belongs_to_many :projects, -> { order("projects.id") }, foreign_key: "developer_id", join_table: "developers_projects"
+ has_and_belongs_to_many :projects, -> { order(Arel.sql("projects.id")) }, foreign_key: "developer_id", join_table: "developers_projects"
default_scope OpenStruct.new(call: includes(:projects))
end
diff --git a/activerecord/test/models/membership.rb b/activerecord/test/models/membership.rb
index 09ee7544b3..47cb4d2146 100644
--- a/activerecord/test/models/membership.rb
+++ b/activerecord/test/models/membership.rb
@@ -12,7 +12,7 @@ class CurrentMembership < Membership
end
class SuperMembership < Membership
- belongs_to :member, -> { order("members.id DESC") }
+ belongs_to :member, -> { order(Arel.sql("members.id DESC")) }
belongs_to :club
end
diff --git a/activerecord/test/models/owner.rb b/activerecord/test/models/owner.rb
index 5fa50d9918..ebaafdec5e 100644
--- a/activerecord/test/models/owner.rb
+++ b/activerecord/test/models/owner.rb
@@ -2,7 +2,7 @@
class Owner < ActiveRecord::Base
self.primary_key = :owner_id
- has_many :pets, -> { order "pets.name desc" }
+ has_many :pets, -> { order Arel.sql("pets.name desc") }
has_many :toys, through: :pets
has_many :persons, through: :pets
diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb
index 5cba1e440e..7067f1b6b0 100644
--- a/activerecord/test/models/person.rb
+++ b/activerecord/test/models/person.rb
@@ -19,7 +19,7 @@ class Person < ActiveRecord::Base
has_many :bad_references
has_many :fixed_bad_references, -> { where favourite: true }, class_name: "BadReference"
has_one :favourite_reference, -> { where "favourite=?", true }, class_name: "Reference"
- has_many :posts_with_comments_sorted_by_comment_id, -> { includes(:comments).order("comments.id") }, through: :readers, source: :post
+ has_many :posts_with_comments_sorted_by_comment_id, -> { includes(:comments).order(Arel.sql("comments.id")) }, through: :readers, source: :post
has_many :first_posts, -> { where(id: [1, 2]) }, through: :readers
has_many :jobs, through: :references
diff --git a/activerecord/test/models/pirate.rb b/activerecord/test/models/pirate.rb
index c8617d1cfe..99de37a52f 100644
--- a/activerecord/test/models/pirate.rb
+++ b/activerecord/test/models/pirate.rb
@@ -3,7 +3,7 @@
class Pirate < ActiveRecord::Base
belongs_to :parrot, validate: true
belongs_to :non_validated_parrot, class_name: "Parrot"
- has_and_belongs_to_many :parrots, -> { order("parrots.id ASC") }, validate: true
+ has_and_belongs_to_many :parrots, -> { order(Arel.sql("parrots.id ASC")) }, validate: true
has_and_belongs_to_many :non_validated_parrots, class_name: "Parrot"
has_and_belongs_to_many :parrots_with_method_callbacks, class_name: "Parrot",
before_add: :log_before_add,
@@ -23,7 +23,7 @@ class Pirate < ActiveRecord::Base
has_one :ship
has_one :update_only_ship, class_name: "Ship"
has_one :non_validated_ship, class_name: "Ship"
- has_many :birds, -> { order("birds.id ASC") }
+ has_many :birds, -> { order(Arel.sql("birds.id ASC")) }
has_many :birds_with_method_callbacks, class_name: "Bird",
before_add: :log_before_add,
after_add: :log_after_add,
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 7f064bf3dd..49bbbaaab7 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -35,8 +35,8 @@ class Post < ActiveRecord::Base
def first_comment
super.body
end
- has_one :first_comment, -> { order("id ASC") }, class_name: "Comment"
- has_one :last_comment, -> { order("id desc") }, class_name: "Comment"
+ has_one :first_comment, -> { order(Arel.sql("id ASC")) }, class_name: "Comment"
+ has_one :last_comment, -> { order(Arel.sql("id desc")) }, class_name: "Comment"
scope :with_special_comments, -> { joins(:comments).where(comments: { type: "SpecialComment" }) }
scope :with_very_special_comments, -> { joins(:comments).where(comments: { type: "VerySpecialComment" }) }
@@ -52,7 +52,7 @@ class Post < ActiveRecord::Base
has_many :comments do
def find_most_recent
- order("id DESC").first
+ order(Arel.sql("id DESC")).first
end
def newest
@@ -85,7 +85,7 @@ class Post < ActiveRecord::Base
has_one :very_special_comment
has_one :very_special_comment_with_post, -> { includes(:post) }, class_name: "VerySpecialComment"
- has_one :very_special_comment_with_post_with_joins, -> { joins(:post).order("posts.id") }, class_name: "VerySpecialComment"
+ has_one :very_special_comment_with_post_with_joins, -> { joins(:post).order(Arel.sql("posts.id")) }, class_name: "VerySpecialComment"
has_many :special_comments
has_many :nonexistent_comments, -> { where "comments.id < 0" }, class_name: "Comment"
@@ -319,5 +319,9 @@ class FakeKlass
def arel_attribute(name, table)
table[name]
end
+
+ def enforce_raw_sql_whitelist(*args)
+ # noop
+ end
end
end
diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb
index 846cef625b..9b282a6729 100644
--- a/activerecord/test/models/project.rb
+++ b/activerecord/test/models/project.rb
@@ -2,9 +2,9 @@
class Project < ActiveRecord::Base
belongs_to :mentor
- has_and_belongs_to_many :developers, -> { distinct.order "developers.name desc, developers.id desc" }
+ has_and_belongs_to_many :developers, -> { distinct.order Arel.sql("developers.name desc, developers.id desc") }
has_and_belongs_to_many :readonly_developers, -> { readonly }, class_name: "Developer"
- has_and_belongs_to_many :non_unique_developers, -> { order "developers.name desc, developers.id desc" }, class_name: "Developer"
+ has_and_belongs_to_many :non_unique_developers, -> { order Arel.sql("developers.name desc, developers.id desc") }, class_name: "Developer"
has_and_belongs_to_many :limited_developers, -> { limit 1 }, class_name: "Developer"
has_and_belongs_to_many :developers_named_david, -> { where("name = 'David'").distinct }, class_name: "Developer"
has_and_belongs_to_many :developers_named_david_with_hash_conditions, -> { where(name: "David").distinct }, class_name: "Developer"
diff --git a/activerecord/test/models/tag.rb b/activerecord/test/models/tag.rb
index bc13c3a42d..e0d42f4f66 100644
--- a/activerecord/test/models/tag.rb
+++ b/activerecord/test/models/tag.rb
@@ -11,6 +11,6 @@ end
class OrderedTag < Tag
self.table_name = "tags"
- has_many :taggings, -> { order("taggings.id DESC") }, foreign_key: "tag_id"
+ has_many :taggings, -> { order(Arel.sql("taggings.id DESC")) }, foreign_key: "tag_id"
has_many :tagged_posts, through: :taggings, source: "taggable", source_type: "Post"
end