Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ActiveRecord::Base.all returns a Relation.

Previously it returned an Array.

If you want an array, call e.g. `Post.to_a` rather than `Post.all`. This
is more explicit.

In most cases this should not break existing code, since
Relations use method_missing to delegate unknown methods to #to_a
anyway.
  • Loading branch information...
commit 6a81ccd69d96f36f4322ef927191ab5a35e68d68 1 parent f1afd77
@jonleighton jonleighton authored
Showing with 329 additions and 324 deletions.
  1. +1 −1  activemodel/lib/active_model/observing.rb
  2. +7 −7 activerecord/lib/active_record/querying.rb
  3. +8 −11 activerecord/lib/active_record/scoping/named.rb
  4. +1 −1  activerecord/test/cases/adapters/mysql/reserved_word_test.rb
  5. +1 −1  activerecord/test/cases/adapters/mysql2/reserved_word_test.rb
  6. +1 −1  activerecord/test/cases/associations/belongs_to_associations_test.rb
  7. +19 −19 activerecord/test/cases/associations/cascaded_eager_loading_test.rb
  8. +1 −1  activerecord/test/cases/associations/eager_load_nested_include_test.rb
  9. +7 −7 activerecord/test/cases/associations/eager_singularization_test.rb
  10. +76 −76 activerecord/test/cases/associations/eager_test.rb
  11. +7 −7 activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
  12. +14 −14 activerecord/test/cases/associations/has_many_associations_test.rb
  13. +2 −2 activerecord/test/cases/associations/has_many_through_associations_test.rb
  14. +1 −1  activerecord/test/cases/associations/has_one_associations_test.rb
  15. +7 −7 activerecord/test/cases/associations/has_one_through_associations_test.rb
  16. +17 −17 activerecord/test/cases/associations/join_model_test.rb
  17. +33 −25 activerecord/test/cases/base_test.rb
  18. +1 −1  activerecord/test/cases/batches_test.rb
  19. +1 −1  activerecord/test/cases/calculations_test.rb
  20. +10 −10 activerecord/test/cases/deprecated_dynamic_methods_test.rb
  21. +4 −4 activerecord/test/cases/explain_test.rb
  22. +17 −17 activerecord/test/cases/finder_test.rb
  23. +6 −6 activerecord/test/cases/inheritance_test.rb
  24. +6 −6 activerecord/test/cases/log_subscriber_test.rb
  25. +2 −2 activerecord/test/cases/migration/rename_column_test.rb
  26. +28 −28 activerecord/test/cases/named_scope_test.rb
  27. +1 −1  activerecord/test/cases/persistence_test.rb
  28. +2 −2 activerecord/test/cases/readonly_test.rb
  29. +23 −23 activerecord/test/cases/relation_scoping_test.rb
  30. +24 −24 activerecord/test/cases/relations_test.rb
  31. +1 −1  activerecord/test/cases/xml_serialization_test.rb
View
2  activemodel/lib/active_model/observing.rb
@@ -215,7 +215,7 @@ def observe(*models)
# end
# end
def observed_classes
- Array(observed_class)
+ [observed_class].compact.flatten
end
# The class observed by default is inferred from the observer's class name:
View
14 activerecord/lib/active_record/querying.rb
@@ -3,15 +3,15 @@
module ActiveRecord
module Querying
- delegate :find, :take, :take!, :first, :first!, :last, :last!, :all, :exists?, :any?, :many?, :to => :scoped
- delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :scoped
- delegate :find_by, :find_by!, :to => :scoped
- delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :scoped
- delegate :find_each, :find_in_batches, :to => :scoped
+ delegate :find, :take, :take!, :first, :first!, :last, :last!, :to_a, :exists?, :any?, :many?, :to => :all
+ delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :all
+ delegate :find_by, :find_by!, :to => :all
+ delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :all
+ delegate :find_each, :find_in_batches, :to => :all
delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins,
:where, :preload, :eager_load, :includes, :from, :lock, :readonly,
- :having, :create_with, :uniq, :references, :none, :to => :scoped
- delegate :count, :average, :minimum, :maximum, :sum, :calculate, :pluck, :ids, :to => :scoped
+ :having, :create_with, :uniq, :references, :none, :to => :all
+ delegate :count, :average, :minimum, :maximum, :sum, :calculate, :pluck, :ids, :to => :all
# Executes a custom SQL query against your database and returns all the results. The results will
# be returned as an array with columns requested encapsulated as attributes of the model you call
View
19 activerecord/lib/active_record/scoping/named.rb
@@ -12,33 +12,30 @@ module Named
extend ActiveSupport::Concern
module ClassMethods
- # Returns an anonymous \scope.
+ # Returns an <tt>ActiveRecord::Relation</tt> scope object.
#
- # posts = Post.scoped
+ # posts = Post.all
# posts.size # Fires "select count(*) from posts" and returns the count
# posts.each {|p| puts p.name } # Fires "select * from posts" and loads post objects
#
- # fruits = Fruit.scoped
+ # fruits = Fruit.all
# fruits = fruits.where(:color => 'red') if options[:red_only]
# fruits = fruits.limit(10) if limited?
#
- # Anonymous \scopes tend to be useful when procedurally generating complex
- # queries, where passing intermediate values (\scopes) around as first-class
- # objects is convenient.
- #
# You can define a \scope that applies to all finders using
# ActiveRecord::Base.default_scope.
- def scoped(options = nil)
+ def all
if current_scope
- scope = current_scope.clone
+ current_scope.clone
else
scope = relation
scope.default_scoped = true
scope
end
+ end
- scope.merge!(options) if options
- scope
+ def scoped(options = nil)
+ options ? all.merge!(options) : all
end
##
View
2  activerecord/test/cases/adapters/mysql/reserved_word_test.rb
@@ -130,7 +130,7 @@ def test_calculations_work_with_reserved_words
end
def test_associations_work_with_reserved_words
- assert_nothing_raised { Select.scoped(:includes => [:groups]).all }
+ assert_nothing_raised { Select.scoped(:includes => [:groups]).to_a }
end
#the following functions were added to DRY test cases
View
2  activerecord/test/cases/adapters/mysql2/reserved_word_test.rb
@@ -130,7 +130,7 @@ def test_calculations_work_with_reserved_words
end
def test_associations_work_with_reserved_words
- assert_nothing_raised { Select.scoped(:includes => [:groups]).all }
+ assert_nothing_raised { Select.scoped(:includes => [:groups]).to_a }
end
#the following functions were added to DRY test cases
View
2  activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -396,7 +396,7 @@ def test_custom_counter_cache
def test_association_assignment_sticks
post = Post.first
- author1, author2 = Author.scoped(:limit => 2).all
+ author1, author2 = Author.scoped(:limit => 2).to_a
assert_not_nil author1
assert_not_nil author2
View
38 activerecord/test/cases/associations/cascaded_eager_loading_test.rb
@@ -16,7 +16,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
:categorizations, :people, :categories, :edges, :vertices
def test_eager_association_loading_with_cascaded_two_levels
- authors = Author.scoped(:includes=>{:posts=>:comments}, :order=>"authors.id").all
+ authors = Author.scoped(:includes=>{:posts=>:comments}, :order=>"authors.id").to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
@@ -24,7 +24,7 @@ def test_eager_association_loading_with_cascaded_two_levels
end
def test_eager_association_loading_with_cascaded_two_levels_and_one_level
- authors = Author.scoped(:includes=>[{:posts=>:comments}, :categorizations], :order=>"authors.id").all
+ authors = Author.scoped(:includes=>[{:posts=>:comments}, :categorizations], :order=>"authors.id").to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
@@ -35,16 +35,16 @@ def test_eager_association_loading_with_cascaded_two_levels_and_one_level
def test_eager_association_loading_with_hmt_does_not_table_name_collide_when_joining_associations
assert_nothing_raised do
- Author.joins(:posts).eager_load(:comments).where(:posts => {:taggings_count => 1}).all
+ Author.joins(:posts).eager_load(:comments).where(:posts => {:taggings_count => 1}).to_a
end
- authors = Author.joins(:posts).eager_load(:comments).where(:posts => {:taggings_count => 1}).all
+ authors = Author.joins(:posts).eager_load(:comments).where(:posts => {:taggings_count => 1}).to_a
assert_equal 1, assert_no_queries { authors.size }
assert_equal 10, assert_no_queries { authors[0].comments.size }
end
def test_eager_association_loading_grafts_stashed_associations_to_correct_parent
assert_nothing_raised do
- Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').order('people.id').all
+ Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').order('people.id').to_a
end
assert_equal people(:michael), Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').order('people.id').first
end
@@ -54,9 +54,9 @@ def test_cascaded_eager_association_loading_with_join_for_count
assert_nothing_raised do
assert_equal 4, categories.count
- assert_equal 4, categories.all.count
+ assert_equal 4, categories.to_a.count
assert_equal 3, categories.count(:distinct => true)
- assert_equal 3, categories.all.uniq.size # Must uniq since instantiating with inner joins will get dupes
+ assert_equal 3, categories.to_a.uniq.size # Must uniq since instantiating with inner joins will get dupes
end
end
@@ -64,7 +64,7 @@ def test_cascaded_eager_association_loading_with_duplicated_includes
categories = Category.includes(:categorizations).includes(:categorizations => :author).where("categorizations.id is not null").references(:categorizations)
assert_nothing_raised do
assert_equal 3, categories.count
- assert_equal 3, categories.all.size
+ assert_equal 3, categories.to_a.size
end
end
@@ -72,7 +72,7 @@ def test_cascaded_eager_association_loading_with_twice_includes_edge_cases
categories = Category.includes(:categorizations => :author).includes(:categorizations => :post).where("posts.id is not null").references(:posts)
assert_nothing_raised do
assert_equal 3, categories.count
- assert_equal 3, categories.all.size
+ assert_equal 3, categories.to_a.size
end
end
@@ -80,11 +80,11 @@ def test_eager_association_loading_with_join_for_count
authors = Author.joins(:special_posts).includes([:posts, :categorizations])
assert_nothing_raised { authors.count }
- assert_queries(3) { authors.all }
+ assert_queries(3) { authors.to_a }
end
def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
- authors = Author.scoped(:includes=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id").all
+ authors = Author.scoped(:includes=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id").to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 3, authors[1].posts.size
@@ -92,7 +92,7 @@ def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_as
end
def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference
- authors = Author.scoped(:includes=>{:posts=>[:comments, :author]}, :order=>"authors.id").all
+ authors = Author.scoped(:includes=>{:posts=>[:comments, :author]}, :order=>"authors.id").to_a
assert_equal 3, authors.size
assert_equal 5, authors[0].posts.size
assert_equal authors(:david).name, authors[0].name
@@ -100,13 +100,13 @@ def test_eager_association_loading_with_cascaded_two_levels_and_self_table_refer
end
def test_eager_association_loading_with_cascaded_two_levels_with_condition
- authors = Author.scoped(:includes=>{:posts=>:comments}, :where=>"authors.id=1", :order=>"authors.id").all
+ authors = Author.scoped(:includes=>{:posts=>:comments}, :where=>"authors.id=1", :order=>"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.scoped(:includes=>{:account=>{:firm=>:account}}, :order=>"companies.id").all
+ firms = Firm.scoped(:includes=>{:account=>{:firm=>:account}}, :order=>"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 }
@@ -114,7 +114,7 @@ def test_eager_association_loading_with_cascaded_three_levels_by_ping_pong
end
def test_eager_association_loading_with_has_many_sti
- topics = Topic.scoped(:includes => :replies, :order => 'topics.id').all
+ topics = Topic.scoped(:includes => :replies, :order => '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
@@ -127,7 +127,7 @@ def test_eager_association_loading_with_has_many_sti_and_subclasses
silly.parent_id = 1
assert silly.save
- topics = Topic.scoped(:includes => :replies, :order => ['topics.id', 'replies_topics.id']).all
+ topics = Topic.scoped(:includes => :replies, :order => ['topics.id', 'replies_topics.id']).to_a
assert_no_queries do
assert_equal 2, topics[0].replies.size
assert_equal 0, topics[1].replies.size
@@ -135,7 +135,7 @@ def test_eager_association_loading_with_has_many_sti_and_subclasses
end
def test_eager_association_loading_with_belongs_to_sti
- replies = Reply.scoped(:includes => :topic, :order => 'topics.id').all
+ replies = Reply.scoped(:includes => :topic, :order => 'topics.id').to_a
assert replies.include?(topics(:second))
assert !replies.include?(topics(:first))
assert_equal topics(:first), assert_no_queries { replies.first.topic }
@@ -151,7 +151,7 @@ def test_eager_association_loading_with_multiple_stis_and_order
end
def test_eager_association_loading_of_stis_with_multiple_references
- authors = Author.scoped(:includes => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :where => 'posts.id = 4').all
+ authors = Author.scoped(:includes => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => '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
@@ -160,7 +160,7 @@ def test_eager_association_loading_of_stis_with_multiple_references
end
def test_eager_association_loading_where_first_level_returns_nil
- authors = Author.scoped(:includes => {:post_about_thinking => :comments}, :order => 'authors.id DESC').all
+ authors = Author.scoped(:includes => {:post_about_thinking => :comments}, :order => '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
View
2  activerecord/test/cases/associations/eager_load_nested_include_test.rb
@@ -92,7 +92,7 @@ def generate_test_object_graphs
end
def test_include_query
- res = ShapeExpression.scoped(:includes => [ :shape, { :paint => :non_poly } ]).all
+ res = ShapeExpression.scoped(:includes => [ :shape, { :paint => :non_poly } ]).to_a
assert_equal NUM_SHAPE_EXPRESSIONS, res.size
assert_queries(0) do
res.each do |se|
View
14 activerecord/test/cases/associations/eager_singularization_test.rb
@@ -103,43 +103,43 @@ def teardown
def test_eager_no_extra_singularization_belongs_to
return unless @have_tables
assert_nothing_raised do
- Virus.scoped(:includes => :octopus).all
+ Virus.scoped(:includes => :octopus).to_a
end
end
def test_eager_no_extra_singularization_has_one
return unless @have_tables
assert_nothing_raised do
- Octopus.scoped(:includes => :virus).all
+ Octopus.scoped(:includes => :virus).to_a
end
end
def test_eager_no_extra_singularization_has_many
return unless @have_tables
assert_nothing_raised do
- Bus.scoped(:includes => :passes).all
+ Bus.scoped(:includes => :passes).to_a
end
end
def test_eager_no_extra_singularization_has_and_belongs_to_many
return unless @have_tables
assert_nothing_raised do
- Crisis.scoped(:includes => :messes).all
- Mess.scoped(:includes => :crises).all
+ Crisis.scoped(:includes => :messes).to_a
+ Mess.scoped(:includes => :crises).to_a
end
end
def test_eager_no_extra_singularization_has_many_through_belongs_to
return unless @have_tables
assert_nothing_raised do
- Crisis.scoped(:includes => :successes).all
+ Crisis.scoped(:includes => :successes).to_a
end
end
def test_eager_no_extra_singularization_has_many_through_has_many
return unless @have_tables
assert_nothing_raised do
- Crisis.scoped(:includes => :compresses).all
+ Crisis.scoped(:includes => :compresses).to_a
end
end
end
View
152 activerecord/test/cases/associations/eager_test.rb
@@ -35,7 +35,7 @@ def test_eager_with_has_one_through_join_model_with_conditions_on_the_through
end
def test_loading_with_one_association
- posts = Post.scoped(:includes => :comments).all
+ posts = Post.scoped(:includes => :comments).to_a
post = posts.find { |p| p.id == 1 }
assert_equal 2, post.comments.size
assert post.comments.include?(comments(:greetings))
@@ -44,13 +44,13 @@ def test_loading_with_one_association
assert_equal 2, post.comments.size
assert post.comments.include?(comments(:greetings))
- posts = Post.scoped(:includes => :last_comment).all
+ posts = Post.scoped(:includes => :last_comment).to_a
post = posts.find { |p| p.id == 1 }
assert_equal Post.find(1).last_comment, post.last_comment
end
def test_loading_with_one_association_with_non_preload
- posts = Post.scoped(:includes => :last_comment, :order => 'comments.id DESC').all
+ posts = Post.scoped(:includes => :last_comment, :order => 'comments.id DESC').to_a
post = posts.find { |p| p.id == 1 }
assert_equal Post.find(1).last_comment, post.last_comment
end
@@ -59,13 +59,13 @@ def test_loading_conditions_with_or
posts = authors(:david).posts.references(:comments).scoped(
:includes => :comments,
:where => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'"
- ).all
+ ).to_a
assert_nil posts.detect { |p| p.author_id != authors(:david).id },
"expected to find only david's posts"
end
def test_with_ordering
- list = Post.scoped(:includes => :comments, :order => "posts.id DESC").all
+ list = Post.scoped(:includes => :comments, :order => "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|
@@ -79,14 +79,14 @@ def test_with_two_tables_in_from_without_getting_double_quoted
end
def test_loading_with_multiple_associations
- posts = Post.scoped(:includes => [ :comments, :author, :categories ], :order => "posts.id").all
+ posts = Post.scoped(:includes => [ :comments, :author, :categories ], :order => "posts.id").to_a
assert_equal 2, posts.first.comments.size
assert_equal 2, posts.first.categories.size
assert posts.first.comments.include?(comments(:greetings))
end
def test_duplicate_middle_objects
- comments = Comment.scoped(:where => 'post_id = 1', :includes => [:post => :author]).all
+ comments = Comment.scoped(:where => 'post_id = 1', :includes => [:post => :author]).to_a
assert_no_queries do
comments.each {|comment| comment.post.author.name}
end
@@ -94,25 +94,25 @@ def test_duplicate_middle_objects
def test_preloading_has_many_in_multiple_queries_with_more_ids_than_database_can_handle
Post.connection.expects(:in_clause_length).at_least_once.returns(5)
- posts = Post.scoped(:includes=>:comments).all
+ posts = Post.scoped(:includes=>:comments).to_a
assert_equal 11, posts.size
end
def test_preloading_has_many_in_one_queries_when_database_has_no_limit_on_ids_it_can_handle
Post.connection.expects(:in_clause_length).at_least_once.returns(nil)
- posts = Post.scoped(:includes=>:comments).all
+ posts = Post.scoped(:includes=>:comments).to_a
assert_equal 11, posts.size
end
def test_preloading_habtm_in_multiple_queries_with_more_ids_than_database_can_handle
Post.connection.expects(:in_clause_length).at_least_once.returns(5)
- posts = Post.scoped(:includes=>:categories).all
+ posts = Post.scoped(:includes=>:categories).to_a
assert_equal 11, posts.size
end
def test_preloading_habtm_in_one_queries_when_database_has_no_limit_on_ids_it_can_handle
Post.connection.expects(:in_clause_length).at_least_once.returns(nil)
- posts = Post.scoped(:includes=>:categories).all
+ posts = Post.scoped(:includes=>:categories).to_a
assert_equal 11, posts.size
end
@@ -150,7 +150,7 @@ def test_including_duplicate_objects_from_belongs_to
popular_post.readers.create!(:person => people(:david))
readers = Reader.scoped(:where => ["post_id = ?", popular_post.id],
- :includes => {:post => :comments}).all
+ :includes => {:post => :comments}).to_a
readers.each do |reader|
assert_equal [comment], reader.post.comments
end
@@ -163,7 +163,7 @@ def test_including_duplicate_objects_from_has_many
comment = car_post.comments.create!(:body => "hmm")
categories = Category.scoped(:where => { 'posts.id' => car_post.id },
- :includes => {:posts => :comments}).all
+ :includes => {:posts => :comments}).to_a
categories.each do |category|
assert_equal [comment], category.posts[0].comments
end
@@ -229,13 +229,13 @@ def test_finding_with_includes_on_null_belongs_to_polymorphic_association
end
def test_loading_from_an_association
- posts = authors(:david).posts.scoped(:includes => :comments, :order => "posts.id").all
+ posts = authors(:david).posts.scoped(:includes => :comments, :order => "posts.id").to_a
assert_equal 2, posts.first.comments.size
end
def test_loading_from_an_association_that_has_a_hash_of_conditions
assert_nothing_raised do
- Author.scoped(:includes => :hello_posts_with_hash_conditions).all
+ Author.scoped(:includes => :hello_posts_with_hash_conditions).to_a
end
assert !Author.scoped(:includes => :hello_posts_with_hash_conditions).find(authors(:david).id).hello_posts.empty?
end
@@ -295,12 +295,12 @@ def test_nested_loading_through_has_one_association_with_conditions_on_nested_as
end
def test_eager_association_loading_with_belongs_to_and_foreign_keys
- pets = Pet.scoped(:includes => :owner).all
+ pets = Pet.scoped(:includes => :owner).to_a
assert_equal 3, pets.length
end
def test_eager_association_loading_with_belongs_to
- comments = Comment.scoped(:includes => :post).all
+ comments = Comment.scoped(:includes => :post).to_a
assert_equal 11, comments.length
titles = comments.map { |c| c.post.title }
assert titles.include?(posts(:welcome).title)
@@ -308,31 +308,31 @@ def test_eager_association_loading_with_belongs_to
end
def test_eager_association_loading_with_belongs_to_and_limit
- comments = Comment.scoped(:includes => :post, :limit => 5, :order => 'comments.id').all
+ comments = Comment.scoped(:includes => :post, :limit => 5, :order => 'comments.id').to_a
assert_equal 5, comments.length
assert_equal [1,2,3,5,6], comments.collect { |c| c.id }
end
def test_eager_association_loading_with_belongs_to_and_limit_and_conditions
- comments = Comment.scoped(:includes => :post, :where => 'post_id = 4', :limit => 3, :order => 'comments.id').all
+ comments = Comment.scoped(:includes => :post, :where => 'post_id = 4', :limit => 3, :order => 'comments.id').to_a
assert_equal 3, comments.length
assert_equal [5,6,7], comments.collect { |c| c.id }
end
def test_eager_association_loading_with_belongs_to_and_limit_and_offset
- comments = Comment.scoped(:includes => :post, :limit => 3, :offset => 2, :order => 'comments.id').all
+ comments = Comment.scoped(:includes => :post, :limit => 3, :offset => 2, :order => 'comments.id').to_a
assert_equal 3, comments.length
assert_equal [3,5,6], comments.collect { |c| c.id }
end
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions
- comments = Comment.scoped(:includes => :post, :where => 'post_id = 4', :limit => 3, :offset => 1, :order => 'comments.id').all
+ comments = Comment.scoped(:includes => :post, :where => 'post_id = 4', :limit => 3, :offset => 1, :order => 'comments.id').to_a
assert_equal 3, comments.length
assert_equal [6,7,8], comments.collect { |c| c.id }
end
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
- comments = Comment.scoped(:includes => :post, :where => ['post_id = ?',4], :limit => 3, :offset => 1, :order => 'comments.id').all
+ comments = Comment.scoped(:includes => :post, :where => ['post_id = ?',4], :limit => 3, :offset => 1, :order => 'comments.id').to_a
assert_equal 3, comments.length
assert_equal [6,7,8], comments.collect { |c| c.id }
end
@@ -340,7 +340,7 @@ def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_cond
def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name
assert_nothing_raised do
ActiveSupport::Deprecation.silence do
- Comment.scoped(:includes => :post, :where => ['posts.id = ?',4]).all
+ Comment.scoped(:includes => :post, :where => ['posts.id = ?',4]).to_a
end
end
end
@@ -348,7 +348,7 @@ def test_eager_association_loading_with_belongs_to_and_conditions_string_with_un
def test_eager_association_loading_with_belongs_to_and_conditions_hash
comments = []
assert_nothing_raised do
- comments = Comment.scoped(:includes => :post, :where => {:posts => {:id => 4}}, :limit => 3, :order => 'comments.id').all
+ comments = Comment.scoped(:includes => :post, :where => {:posts => {:id => 4}}, :limit => 3, :order => 'comments.id').to_a
end
assert_equal 3, comments.length
assert_equal [5,6,7], comments.collect { |c| c.id }
@@ -361,14 +361,14 @@ def test_eager_association_loading_with_belongs_to_and_conditions_string_with_qu
quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
assert_nothing_raised do
ActiveSupport::Deprecation.silence do
- Comment.scoped(:includes => :post, :where => ["#{quoted_posts_id} = ?",4]).all
+ Comment.scoped(:includes => :post, :where => ["#{quoted_posts_id} = ?",4]).to_a
end
end
end
def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
assert_nothing_raised do
- Comment.scoped(:includes => :post, :order => 'posts.id').all
+ Comment.scoped(:includes => :post, :order => 'posts.id').to_a
end
end
@@ -376,19 +376,19 @@ def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_
quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
assert_nothing_raised do
ActiveSupport::Deprecation.silence do
- Comment.scoped(:includes => :post, :order => quoted_posts_id).all
+ Comment.scoped(:includes => :post, :order => quoted_posts_id).to_a
end
end
end
def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
- posts = Post.scoped(:includes => [:author, :very_special_comment], :limit => 1, :order => 'posts.id').all
+ posts = Post.scoped(:includes => [:author, :very_special_comment], :limit => 1, :order => 'posts.id').to_a
assert_equal 1, posts.length
assert_equal [1], posts.collect { |p| p.id }
end
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
- posts = Post.scoped(:includes => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id').all
+ posts = Post.scoped(:includes => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id').to_a
assert_equal 1, posts.length
assert_equal [2], posts.collect { |p| p.id }
end
@@ -453,14 +453,14 @@ def test_eager_load_belongs_to_with_string_keys
end
def test_eager_association_loading_with_explicit_join
- posts = Post.scoped(:includes => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id').all
+ posts = Post.scoped(:includes => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id').to_a
assert_equal 1, posts.length
end
def test_eager_with_has_many_through
- posts_with_comments = people(:michael).posts.scoped(:includes => :comments, :order => 'posts.id').all
- posts_with_author = people(:michael).posts.scoped(:includes => :author, :order => 'posts.id').all
- posts_with_comments_and_author = people(:michael).posts.scoped(:includes => [ :comments, :author ], :order => 'posts.id').all
+ posts_with_comments = people(:michael).posts.scoped(:includes => :comments, :order => 'posts.id').to_a
+ posts_with_author = people(:michael).posts.scoped(:includes => :author, :order => 'posts.id').to_a
+ posts_with_comments_and_author = people(:michael).posts.scoped(:includes => [ :comments, :author ], :order => '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 }
@@ -471,7 +471,7 @@ def test_eager_with_has_many_through_a_belongs_to_association
Post.create!(:author => author, :title => "TITLE", :body => "BODY")
author.author_favorites.create(:favorite_author_id => 1)
author.author_favorites.create(:favorite_author_id => 2)
- posts_with_author_favorites = author.posts.scoped(:includes => :author_favorites).all
+ posts_with_author_favorites = author.posts.scoped(:includes => :author_favorites).to_a
assert_no_queries { posts_with_author_favorites.first.author_favorites.first.author_id }
end
@@ -515,16 +515,16 @@ def test_eager_with_has_many_through_join_model_ignores_default_includes
end
def test_eager_with_has_many_and_limit
- posts = Post.scoped(:order => 'posts.id asc', :includes => [ :author, :comments ], :limit => 2).all
+ posts = Post.scoped(:order => '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
if current_adapter?(:OpenBaseAdapter)
- posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => "FETCHBLOB(posts.body) = 'hello'", :order => "posts.id").all
+ posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => "FETCHBLOB(posts.body) = 'hello'", :order => "posts.id").to_a
else
- posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => "posts.body = 'hello'", :order => "posts.id").all
+ posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => "posts.body = 'hello'", :order => "posts.id").to_a
end
assert_equal 2, posts.size
assert_equal [4,5], posts.collect { |p| p.id }
@@ -532,9 +532,9 @@ def test_eager_with_has_many_and_limit_and_conditions
def test_eager_with_has_many_and_limit_and_conditions_array
if current_adapter?(:OpenBaseAdapter)
- posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => [ "FETCHBLOB(posts.body) = ?", 'hello' ], :order => "posts.id").all
+ posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => [ "FETCHBLOB(posts.body) = ?", 'hello' ], :order => "posts.id").to_a
else
- posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => [ "posts.body = ?", 'hello' ], :order => "posts.id").all
+ posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => [ "posts.body = ?", 'hello' ], :order => "posts.id").to_a
end
assert_equal 2, posts.size
assert_equal [4,5], posts.collect { |p| p.id }
@@ -542,7 +542,7 @@ def test_eager_with_has_many_and_limit_and_conditions_array
def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
posts = ActiveSupport::Deprecation.silence do
- Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => [ "authors.name = ?", 'David' ]).all
+ Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => [ "authors.name = ?", 'David' ]).to_a
end
assert_equal 2, posts.size
@@ -553,7 +553,7 @@ def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
end
def test_eager_with_has_many_and_limit_and_high_offset
- posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :offset => 10, :where => { 'authors.name' => 'David' }).all
+ posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :offset => 10, :where => { 'authors.name' => 'David' }).to_a
assert_equal 0, posts.size
end
@@ -561,7 +561,7 @@ def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_condit
assert_queries(1) do
posts = Post.references(:authors, :comments).
scoped(:includes => [ :author, :comments ], :limit => 2, :offset => 10,
- :where => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ]).all
+ :where => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ]).to_a
assert_equal 0, posts.size
end
end
@@ -569,7 +569,7 @@ def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_condit
def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash_conditions
assert_queries(1) do
posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :offset => 10,
- :where => { 'authors.name' => 'David', 'comments.body' => 'go crazy' }).all
+ :where => { 'authors.name' => 'David', 'comments.body' => 'go crazy' }).to_a
assert_equal 0, posts.size
end
end
@@ -580,7 +580,7 @@ def test_count_eager_with_has_many_and_limit_and_high_offset
end
def test_eager_with_has_many_and_limit_with_no_results
- posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => "posts.title = 'magic forest'").all
+ posts = Post.scoped(:includes => [ :author, :comments ], :limit => 2, :where => "posts.title = 'magic forest'").to_a
assert_equal 0, posts.size
end
@@ -597,7 +597,7 @@ def test_eager_count_performed_on_a_has_many_through_association_with_multi_tabl
end
def test_eager_with_has_and_belongs_to_many_and_limit
- posts = Post.scoped(:includes => :categories, :order => "posts.id", :limit => 3).all
+ posts = Post.scoped(:includes => :categories, :order => "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
@@ -663,7 +663,7 @@ def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
end
def test_eager_association_loading_with_habtm
- posts = Post.scoped(:includes => :categories, :order => "posts.id").all
+ posts = Post.scoped(:includes => :categories, :order => "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
@@ -672,7 +672,7 @@ def test_eager_association_loading_with_habtm
end
def test_eager_with_inheritance
- SpecialPost.scoped(:includes => [ :comments ]).all
+ SpecialPost.scoped(:includes => [ :comments ]).to_a
end
def test_eager_has_one_with_association_inheritance
@@ -727,7 +727,7 @@ def test_eager_with_invalid_association_reference
def test_eager_with_default_scope
developer = EagerDeveloperWithDefaultScope.where(:name => 'David').first
- projects = Project.order(:id).all
+ projects = Project.order(:id).to_a
assert_no_queries do
assert_equal(projects, developer.projects)
end
@@ -735,7 +735,7 @@ def test_eager_with_default_scope
def test_eager_with_default_scope_as_class_method
developer = EagerDeveloperWithClassMethodDefaultScope.where(:name => 'David').first
- projects = Project.order(:id).all
+ projects = Project.order(:id).to_a
assert_no_queries do
assert_equal(projects, developer.projects)
end
@@ -743,7 +743,7 @@ def test_eager_with_default_scope_as_class_method
def test_eager_with_default_scope_as_lambda
developer = EagerDeveloperWithLambdaDefaultScope.where(:name => 'David').first
- projects = Project.order(:id).all
+ projects = Project.order(:id).to_a
assert_no_queries do
assert_equal(projects, developer.projects)
end
@@ -751,7 +751,7 @@ def test_eager_with_default_scope_as_lambda
def test_eager_with_default_scope_as_block
developer = EagerDeveloperWithBlockDefaultScope.where(:name => 'David').first
- projects = Project.order(:id).all
+ projects = Project.order(:id).to_a
assert_no_queries do
assert_equal(projects, developer.projects)
end
@@ -759,14 +759,14 @@ def test_eager_with_default_scope_as_block
def test_eager_with_default_scope_as_callable
developer = EagerDeveloperWithCallableDefaultScope.where(:name => 'David').first
- projects = Project.order(:id).all
+ projects = Project.order(:id).to_a
assert_no_queries do
assert_equal(projects, developer.projects)
end
end
def find_all_ordered(className, include=nil)
- className.scoped(:order=>"#{className.table_name}.#{className.primary_key}", :includes=>include).all
+ className.scoped(:order=>"#{className.table_name}.#{className.primary_key}", :includes=>include).to_a
end
def test_limited_eager_with_order
@@ -775,14 +775,14 @@ def test_limited_eager_with_order
Post.scoped(
:includes => [:author, :comments], :where => { 'authors.name' => 'David' },
:order => 'UPPER(posts.title)', :limit => 2, :offset => 1
- ).all
+ ).to_a
)
assert_equal(
posts(:sti_post_and_comments, :sti_comments),
Post.scoped(
:includes => [:author, :comments], :where => { 'authors.name' => 'David' },
:order => 'UPPER(posts.title) DESC', :limit => 2, :offset => 1
- ).all
+ ).to_a
)
end
@@ -792,14 +792,14 @@ def test_limited_eager_with_multiple_order_columns
Post.scoped(
:includes => [:author, :comments], :where => { 'authors.name' => 'David' },
:order => ['UPPER(posts.title)', 'posts.id'], :limit => 2, :offset => 1
- ).all
+ ).to_a
)
assert_equal(
posts(:sti_post_and_comments, :sti_comments),
Post.scoped(
:includes => [:author, :comments], :where => { 'authors.name' => 'David' },
:order => ['UPPER(posts.title) DESC', 'posts.id'], :limit => 2, :offset => 1
- ).all
+ ).to_a
)
end
@@ -810,7 +810,7 @@ def test_limited_eager_with_numeric_in_association
:includes => [:readers, :primary_contact, :number1_fan],
:where => "number1_fans_people.first_name like 'M%'",
:order => 'people.id', :limit => 2, :offset => 0
- ).all
+ ).to_a
)
end
@@ -876,13 +876,13 @@ def test_eager_with_multiple_associations_with_same_table_belongs_to
end
end
def test_eager_with_valid_association_as_string_not_symbol
- assert_nothing_raised { Post.scoped(:includes => 'comments').all }
+ assert_nothing_raised { Post.scoped(:includes => 'comments').to_a }
end
def test_eager_with_floating_point_numbers
assert_queries(2) do
# Before changes, the floating point numbers will be interpreted as table names and will cause this to run in one query
- Comment.scoped(:where => "123.456 = 123.456", :includes => :post).all
+ Comment.scoped(:where => "123.456 = 123.456", :includes => :post).to_a
end
end
@@ -936,21 +936,21 @@ def test_count_with_include
def test_load_with_sti_sharing_association
assert_queries(2) do #should not do 1 query per subclass
- Comment.includes(:post).all
+ Comment.includes(:post).to_a
end
end
def test_conditions_on_join_table_with_include_and_limit
- assert_equal 3, Developer.scoped(:includes => 'projects', :where => { 'developers_projects.access_level' => 1 }, :limit => 5).all.size
+ assert_equal 3, Developer.scoped(:includes => 'projects', :where => { 'developers_projects.access_level' => 1 }, :limit => 5).to_a.size
end
def test_order_on_join_table_with_include_and_limit
- assert_equal 5, Developer.scoped(:includes => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).all.size
+ assert_equal 5, Developer.scoped(:includes => 'projects', :order => '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.scoped(:joins => :comments, :includes => :author, :order => 'comments.id DESC').all
+ Post.scoped(:joins => :comments, :includes => :author, :order => 'comments.id DESC').to_a
end
assert_equal posts(:eager_other), posts[1]
assert_equal authors(:mary), assert_no_queries { posts[1].author}
@@ -958,37 +958,37 @@ def test_eager_loading_with_order_on_joined_table_preloads
def test_eager_loading_with_conditions_on_joined_table_preloads
posts = assert_queries(2) do
- Post.scoped(:select => 'distinct posts.*', :includes => :author, :joins => [:comments], :where => "comments.body like 'Thank you%'", :order => 'posts.id').all
+ Post.scoped(:select => 'distinct posts.*', :includes => :author, :joins => [:comments], :where => "comments.body like 'Thank you%'", :order => '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.scoped(:select => 'distinct posts.*', :includes => :author, :joins => [:comments], :where => "comments.body like 'Thank you%'", :order => 'posts.id').all
+ Post.scoped(:select => 'distinct posts.*', :includes => :author, :joins => [:comments], :where => "comments.body like 'Thank you%'", :order => '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.scoped(:includes => :author, :joins => {:taggings => :tag}, :where => "tags.name = 'General'", :order => 'posts.id').all
+ Post.scoped(:includes => :author, :joins => {:taggings => :tag}, :where => "tags.name = 'General'", :order => 'posts.id').to_a
end
assert_equal posts(:welcome, :thinking), posts
posts = assert_queries(2) do
- Post.scoped(:includes => :author, :joins => {:taggings => {:tag => :taggings}}, :where => "taggings_tags.super_tag_id=2", :order => 'posts.id').all
+ Post.scoped(:includes => :author, :joins => {:taggings => {:tag => :taggings}}, :where => "taggings_tags.super_tag_id=2", :order => 'posts.id').to_a
end
assert_equal posts(:welcome, :thinking), posts
end
def test_eager_loading_with_conditions_on_string_joined_table_preloads
posts = assert_queries(2) do
- Post.scoped(:select => 'distinct posts.*', :includes => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :where => "comments.body like 'Thank you%'", :order => 'posts.id').all
+ Post.scoped(: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
end
assert_equal [posts(:welcome)], posts
assert_equal authors(:david), assert_no_queries { posts[0].author}
posts = assert_queries(2) do
- Post.scoped(:select => 'distinct posts.*', :includes => :author, :joins => ["INNER JOIN comments on comments.post_id = posts.id"], :where => "comments.body like 'Thank you%'", :order => 'posts.id').all
+ Post.scoped(: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
end
assert_equal [posts(:welcome)], posts
assert_equal authors(:david), assert_no_queries { posts[0].author}
@@ -996,7 +996,7 @@ def test_eager_loading_with_conditions_on_string_joined_table_preloads
def test_eager_loading_with_select_on_joined_table_preloads
posts = assert_queries(2) do
- Post.scoped(:select => 'posts.*, authors.name as author_name', :includes => :comments, :joins => :author, :order => 'posts.id').all
+ Post.scoped(:select => 'posts.*, authors.name as author_name', :includes => :comments, :joins => :author, :order => 'posts.id').to_a
end
assert_equal 'David', posts[0].author_name
assert_equal posts(:welcome).comments, assert_no_queries { posts[0].comments}
@@ -1004,14 +1004,14 @@ def test_eager_loading_with_select_on_joined_table_preloads
def test_eager_loading_with_conditions_on_join_model_preloads
authors = assert_queries(2) do
- Author.scoped(:includes => :author_address, :joins => :comments, :where => "posts.title like 'Welcome%'").all
+ Author.scoped(:includes => :author_address, :joins => :comments, :where => "posts.title like 'Welcome%'").to_a
end
assert_equal authors(:david), authors[0]
assert_equal author_addresses(:david_address), authors[0].author_address
end
def test_preload_belongs_to_uses_exclusive_scope
- people = Person.males.scoped(:includes => :primary_contact).all
+ people = Person.males.scoped(:includes => :primary_contact).to_a
assert_not_equal people.length, 0
people.each do |person|
assert_no_queries {assert_not_nil person.primary_contact}
@@ -1020,7 +1020,7 @@ def test_preload_belongs_to_uses_exclusive_scope
end
def test_preload_has_many_uses_exclusive_scope
- people = Person.males.includes(:agents).all
+ people = Person.males.includes(:agents).to_a
people.each do |person|
assert_equal Person.find(person.id).agents, person.agents
end
@@ -1057,7 +1057,7 @@ def test_preload_has_one_using_primary_key
def test_include_has_one_using_primary_key
expected = accounts(:signals37)
- firm = Firm.scoped(:includes => :account_using_primary_key, :order => 'accounts.id').all.detect {|f| f.id == 1}
+ firm = Firm.scoped(:includes => :account_using_primary_key, :order => 'accounts.id').to_a.detect {|f| f.id == 1}
assert_no_queries do
assert_equal expected, firm.account_using_primary_key
end
@@ -1121,7 +1121,7 @@ def test_join_eager_with_nil_order_should_generate_valid_sql
end
def test_deep_including_through_habtm
- posts = Post.scoped(:includes => {:categories => :categorizations}, :order => "posts.id").all
+ posts = Post.scoped(:includes => {:categories => :categorizations}, :order => "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 }
View
14 activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -346,7 +346,7 @@ def test_deleting
def test_deleting_array
david = Developer.find(1)
david.projects.reload
- david.projects.delete(Project.all)
+ david.projects.delete(Project.to_a)
assert_equal 0, david.projects.size
assert_equal 0, david.projects(true).size
end
@@ -388,7 +388,7 @@ def test_destroying
def test_destroying_many
david = Developer.find(1)
david.projects.reload
- projects = Project.all
+ projects = Project.to_a
assert_no_difference "Project.count" do
david.projects.destroy(*projects)
@@ -501,8 +501,8 @@ def test_include_returns_false_for_non_matching_record_to_verify_scoping
def test_find_with_merged_options
assert_equal 1, projects(:active_record).limited_developers.size
- assert_equal 1, projects(:active_record).limited_developers.all.size
- assert_equal 3, projects(:active_record).limited_developers.limit(nil).all.size
+ assert_equal 1, projects(:active_record).limited_developers.to_a.size
+ assert_equal 3, projects(:active_record).limited_developers.limit(nil).to_a.size
end
def test_dynamic_find_should_respect_association_order
@@ -536,7 +536,7 @@ def test_new_with_values_in_collection
end
def test_find_in_association_with_options
- developers = projects(:active_record).developers.all
+ developers = projects(:active_record).developers.to_a
assert_equal 3, developers.size
assert_equal developers(:poor_jamis), projects(:active_record).developers.where("salary < 10000").first
@@ -638,8 +638,8 @@ def test_join_with_group
end
def test_find_grouped
- all_posts_from_category1 = Post.scoped(:where => "category_id = 1", :joins => :categories).all
- grouped_posts_of_category1 = Post.scoped(:where => "category_id = 1", :group => "author_id", :select => 'count(posts.id) as posts_count', :joins => :categories).all
+ all_posts_from_category1 = Post.scoped(:where => "category_id = 1", :joins => :categories).to_a
+ grouped_posts_of_category1 = Post.scoped(:where => "category_id = 1", :group => "author_id", :select => 'count(posts.id) as posts_count', :joins => :categories).to_a
assert_equal 5, all_posts_from_category1.size
assert_equal 2, grouped_posts_of_category1.size
end
View
28 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -221,14 +221,14 @@ def test_finding_array_compatibility
def test_find_with_blank_conditions
[[], {}, nil, ""].each do |blank|
- assert_equal 2, Firm.scoped(:order => "id").first.clients.where(blank).all.size
+ assert_equal 2, Firm.scoped(:order => "id").first.clients.where(blank).to_a.size
end
end
def test_find_many_with_merged_options
assert_equal 1, companies(:first_firm).limited_clients.size
- assert_equal 1, companies(:first_firm).limited_clients.all.size
- assert_equal 2, companies(:first_firm).limited_clients.limit(nil).all.size
+ assert_equal 1, companies(:first_firm).limited_clients.to_a.size
+ assert_equal 2, companies(:first_firm).limited_clients.limit(nil).to_a.size
end
def test_find_should_append_to_association_order
@@ -299,8 +299,8 @@ def test_find_ids
def test_find_all
firm = Firm.scoped(:order => "id").first
- assert_equal 2, firm.clients.scoped(:where => "#{QUOTED_TYPE} = 'Client'").all.length
- assert_equal 1, firm.clients.scoped(:where => "name = 'Summit'").all.length
+ assert_equal 2, firm.clients.scoped(:where => "#{QUOTED_TYPE} = 'Client'").to_a.length
+ assert_equal 1, firm.clients.scoped(:where => "name = 'Summit'").to_a.length
end
def test_find_each
@@ -345,9 +345,9 @@ def test_find_in_batches
def test_find_all_sanitized
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
firm = Firm.scoped(:order => "id").first
- summit = firm.clients.scoped(:where => "name = 'Summit'").all
- assert_equal summit, firm.clients.scoped(:where => ["name = ?", "Summit"]).all
- assert_equal summit, firm.clients.scoped(:where => ["name = :name", { :name => "Summit" }]).all
+ summit = firm.clients.scoped(:where => "name = 'Summit'").to_a
+ assert_equal summit, firm.clients.scoped(:where => ["name = ?", "Summit"]).to_a
+ assert_equal summit, firm.clients.scoped(:where => ["name = :name", { :name => "Summit" }]).to_a
end
def test_find_first
@@ -366,7 +366,7 @@ def test_find_first_sanitized
def test_find_all_with_include_and_conditions
assert_nothing_raised do
- Developer.scoped(:joins => :audit_logs, :where => {'audit_logs.message' => nil, :name => 'Smith'}).all
+ Developer.scoped(:joins => :audit_logs, :where => {'audit_logs.message' => nil, :name => 'Smith'}).to_a
end
end
@@ -376,8 +376,8 @@ def test_find_in_collection
end
def test_find_grouped
- all_clients_of_firm1 = Client.scoped(:where => "firm_id = 1").all
- grouped_clients_of_firm1 = Client.scoped(:where => "firm_id = 1", :group => "firm_id", :select => 'firm_id, count(id) as clients_count').all
+ all_clients_of_firm1 = Client.scoped(:where => "firm_id = 1").to_a
+ grouped_clients_of_firm1 = Client.scoped(:where => "firm_id = 1", :group => "firm_id", :select => 'firm_id, count(id) as clients_count').to_a
assert_equal 2, all_clients_of_firm1.size
assert_equal 1, grouped_clients_of_firm1.size
end
@@ -939,7 +939,7 @@ def test_dependence
firm = companies(:first_firm)
assert_equal 2, firm.clients.size
firm.destroy
- assert Client.scoped(:where => "firm_id=#{firm.id}").all.empty?
+ assert Client.scoped(:where => "firm_id=#{firm.id}").to_a.empty?
end
def test_dependence_for_associations_with_hash_condition
@@ -977,7 +977,7 @@ def test_dependence_with_transaction_support_on_failure
firm.destroy rescue "do nothing"
- assert_equal 2, Client.scoped(:where => "firm_id=#{firm.id}").all.size
+ assert_equal 2, Client.scoped(:where => "firm_id=#{firm.id}").to_a.size
end
def test_dependence_on_account
@@ -1040,7 +1040,7 @@ def test_included_in_collection
end
def test_adding_array_and_collection
- assert_nothing_raised { Firm.first.clients + Firm.all.last.clients }
+ assert_nothing_raised { Firm.first.clients + Firm.to_a.last.clients }
end
def test_replace_with_less
View
4 activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -706,7 +706,7 @@ def test_include_method_in_association_through_should_return_true_for_instance_a
def test_through_association_readonly_should_be_false
assert !people(:michael).posts.first.readonly?
- assert !people(:michael).posts.all.first.readonly?
+ assert !people(:michael).posts.to_a.first.readonly?
end
def test_can_update_through_association
@@ -742,7 +742,7 @@ def test_size_of_through_association_should_increase_correctly_when_has_many_ass
end
def test_has_many_through_with_default_scope_on_join_model
- assert_equal posts(:welcome).comments.order('id').all, authors(:david).comments_on_first_posts
+ assert_equal posts(:welcome).comments.order('id').to_a, authors(:david).comments_on_first_posts
end
def test_create_has_many_through_with_default_scope_on_join_model
View
2  activerecord/test/cases/associations/has_one_associations_test.rb
@@ -25,7 +25,7 @@ def test_has_one_cache_nils
assert_queries(1) { assert_nil firm.account }
assert_queries(0) { assert_nil firm.account }
- firms = Firm.scoped(:includes => :account).all
+ firms = Firm.scoped(:includes => :account).to_a
assert_queries(0) { firms.each(&:account) }
end
View
14 activerecord/test/cases/associations/has_one_through_associations_test.rb
@@ -73,7 +73,7 @@ def test_has_one_through_polymorphic
def test_has_one_through_eager_loading
members = assert_queries(3) do #base table, through table, clubs table
- Member.scoped(:includes => :club, :where => ["name = ?", "Groucho Marx"]).all
+ Member.scoped(:includes => :club, :where => ["name = ?", "Groucho Marx"]).to_a
end
assert_equal 1, members.size
assert_not_nil assert_no_queries {members[0].club}
@@ -81,7 +81,7 @@ def test_has_one_through_eager_loading
def test_has_one_through_eager_loading_through_polymorphic
members = assert_queries(3) do #base table, through table, clubs table
- Member.scoped(:includes => :sponsor_club, :where => ["name = ?", "Groucho Marx"]).all
+ Member.scoped(:includes => :sponsor_club, :where => ["name = ?", "Groucho Marx"]).to_a
end
assert_equal 1, members.size
assert_not_nil assert_no_queries {members[0].sponsor_club}
@@ -104,14 +104,14 @@ def test_has_one_through_polymorphic_with_source_type
end
def test_eager_has_one_through_polymorphic_with_source_type
- clubs = Club.scoped(:includes => :sponsored_member, :where => ["name = ?","Moustache and Eyebrow Fancier Club"]).all
+ clubs = Club.scoped(:includes => :sponsored_member, :where => ["name = ?","Moustache and Eyebrow Fancier Club"]).to_a
# Only the eyebrow fanciers club has a sponsored_member
assert_not_nil assert_no_queries {clubs[0].sponsored_member}
end
def test_has_one_through_nonpreload_eagerloading
members = assert_queries(1) do
- Member.scoped(:includes => :club, :where => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name').all #force fallback
+ Member.scoped(:includes => :club, :where => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name').to_a #force fallback
end
assert_equal 1, members.size
assert_not_nil assert_no_queries {members[0].club}
@@ -119,7 +119,7 @@ def test_has_one_through_nonpreload_eagerloading
def test_has_one_through_nonpreload_eager_loading_through_polymorphic
members = assert_queries(1) do
- Member.scoped(:includes => :sponsor_club, :where => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name').all #force fallback
+ Member.scoped(:includes => :sponsor_club, :where => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name').to_a #force fallback
end
assert_equal 1, members.size
assert_not_nil assert_no_queries {members[0].sponsor_club}
@@ -128,7 +128,7 @@ def test_has_one_through_nonpreload_eager_loading_through_polymorphic
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.scoped(:includes => :sponsor_club, :where => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name DESC').all #force fallback
+ Member.scoped(:includes => :sponsor_club, :where => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name DESC').to_a #force fallback
end
assert_equal 1, members.size
assert_not_nil assert_no_queries { members[0].sponsor_club }
@@ -197,7 +197,7 @@ def test_preloading_has_one_through_on_belongs_to
@member.member_detail = @member_detail
@member.organization = @organization
@member_details = assert_queries(3) do
- MemberDetail.scoped(:includes => :member_type).all
+ MemberDetail.scoped(:includes => :member_type).to_a
end
@new_detail = @member_details[0]
assert @new_detail.send(:association, :member_type).loaded?
View
34 activerecord/test/cases/associations/join_model_test.rb
@@ -51,7 +51,7 @@ def test_has_many_uniq_through_count
end
def test_has_many_uniq_through_find
- assert_equal 1, authors(:mary).unique_categorized_posts.all.size
+ assert_equal 1, authors(:mary).unique_categorized_posts.to_a.size
end
def test_polymorphic_has_many_going_through_join_model
@@ -233,8 +233,8 @@ def test_has_many_with_piggyback
end
def test_include_has_many_through
- posts = Post.scoped(:order => 'posts.id').all
- posts_with_authors = Post.scoped(:includes => :authors, :order => 'posts.id').all
+ posts = Post.scoped(:order => 'posts.id').to_a
+ posts_with_authors = Post.scoped(:includes => :authors, :order => '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 }
@@ -258,8 +258,8 @@ def test_include_polymorphic_has_one_defined_in_abstract_parent
end
def test_include_polymorphic_has_many_through
- posts = Post.scoped(:order => 'posts.id').all
- posts_with_tags = Post.scoped(:includes => :tags, :order => 'posts.id').all
+ posts = Post.scoped(:order => 'posts.id').to_a
+ posts_with_tags = Post.scoped(:includes => :tags, :order => '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 }
@@ -267,8 +267,8 @@ def test_include_polymorphic_has_many_through
end
def test_include_polymorphic_has_many
- posts = Post.scoped(:order => 'posts.id').all
- posts_with_taggings = Post.scoped(:includes => :taggings, :order => 'posts.id').all
+ posts = Post.scoped(:order => 'posts.id').to_a
+ posts_with_taggings = Post.scoped(:includes => :taggings, :order => '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 }
@@ -276,7 +276,7 @@ def test_include_polymorphic_has_many
end
def test_has_many_find_all
- assert_equal [categories(:general)], authors(:david).categories.all
+ assert_equal [categories(:general)], authors(:david).categories.to_a
end
def test_has_many_find_first
@@ -365,11 +365,11 @@ def test_eager_has_many_polymorphic_with_source_type
end
def test_has_many_through_has_many_find_all
- assert_equal comments(:greetings), authors(:david).comments.scoped(:order => 'comments.id').all.first
+ assert_equal comments(:greetings), authors(:david).comments.scoped(:order => '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.scoped(:order => 'comments.id').all.first
+ assert_equal comments(:greetings), authors(:david).funky_comments.scoped(:order => 'comments.id').to_a.first
end
def test_has_many_through_has_many_find_first
@@ -643,8 +643,8 @@ def test_polymorphic_belongs_to
end
def test_preload_polymorphic_has_many_through
- posts = Post.scoped(:order => 'posts.id').all
- posts_with_tags = Post.scoped(:includes => :tags, :order => 'posts.id').all
+ posts = Post.scoped(:order => 'posts.id').to_a
+ posts_with_tags = Post.scoped(:includes => :tags, :order => '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 }
@@ -652,7 +652,7 @@ def test_preload_polymorphic_has_many_through
end
def test_preload_polymorph_many_types
- taggings = Tagging.scoped(:includes => :taggable, :where => ['taggable_type != ?', 'FakeModel']).all
+ taggings = Tagging.scoped(:includes => :taggable, :where => ['taggable_type != ?', 'FakeModel']).to_a
assert_no_queries do
taggings.first.taggable.id
taggings[1].taggable.id
@@ -665,13 +665,13 @@ def test_preload_polymorph_many_types
def test_preload_nil_polymorphic_belongs_to
assert_nothing_raised do
- Tagging.scoped(:includes => :taggable, :where => ['taggable_type IS NULL']).all
+ Tagging.scoped(:includes => :taggable, :where => ['taggable_type IS NULL']).to_a
end
end
def test_preload_polymorphic_has_many
- posts = Post.scoped(:order => 'posts.id').all
- posts_with_taggings = Post.scoped(:includes => :taggings, :order => 'posts.id').all
+ posts = Post.scoped(:order => 'posts.id').to_a
+ posts_with_taggings = Post.scoped(:includes => :taggings, :order => '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 }
@@ -679,7 +679,7 @@ def test_preload_polymorphic_has_many
end
def test_belongs_to_shared_parent
- comments = Comment.scoped(:includes => :post, :where => 'post_id = 1').all
+ comments = Comment.scoped(:includes => :post, :where => 'post_id = 1').to_a
assert_no_queries do
assert_equal comments.first.post, comments[1].post
end
View
58 activerecord/test/cases/base_test.rb
@@ -131,36 +131,36 @@ def test_primary_key_with_no_id
unless current_adapter?(:PostgreSQLAdapter,:OracleAdapter,:SQLServerAdapter)
def test_limit_with_comma
- assert Topic.limit("1,2").all
+ assert Topic.limit("1,2").to_a
end
end
def test_limit_without_comma
- assert_equal 1, Topic.limit("1").all.length
- assert_equal 1, Topic.limit(1).all.length
+ assert_equal 1, Topic.limit("1").to_a.length
+ assert_equal 1, Topic.limit(1).to_a.length
end
def test_invalid_limit
assert_raises(ArgumentError) do
- Topic.limit("asdfadf").all
+ Topic.limit("asdfadf").to_a
end
end
def test_limit_should_sanitize_sql_injection_for_limit_without_comas
assert_raises(ArgumentError) do
- Topic.limit("1 select * from schema").all
+ Topic.limit("1 select * from schema").to_a
end
end
def test_limit_should_sanitize_sql_injection_for_limit_with_comas
assert_raises(ArgumentError) do
- Topic.limit("1, 7 procedure help()").all
+ Topic.limit("1, 7 procedure help()").to_a
end
end
unless current_adapter?(:MysqlAdapter) || current_adapter?(:Mysql2Adapter)
def test_limit_should_allow_sql_literal
- assert_equal 1, Topic.limit(Arel.sql('2-1')).all.length
+ assert_equal 1, Topic.limit(Arel.sql('2-1')).to_a.length
end
end
@@ -349,13 +349,13 @@ def test_first_or_initialize
end
def test_load
- topics = Topic.scoped(:order => 'id').all
+ topics = Topic.scoped(:order => 'id').to_a
assert_equal(4, topics.size)
assert_equal(topics(:first).title, topics.first.title)
end
def test_load_with_condition
- topics = Topic.scoped(:where => "author_name = 'Mary'").all
+ topics = Topic.scoped(:where => "author_name = 'Mary'").to_a
assert_equal(1, topics.size)
assert_equal(topics(:second).title, topics.first.title)
@@ -1218,10 +1218,10 @@ def test_column_name_properly_quoted
end
def test_quoting_arrays
- replies = Reply.scoped(:where => [ "id IN (?)", topics(:first).replies.collect(&:id) ]).all
+ replies = Reply.scoped(:where => [ "id IN (?)", topics(:first).replies.collect(&:id) ]).to_a
assert_equal topics(:first).replies.size, replies.size
- replies = Reply.scoped(:where => [ "id IN (?)", [] ]).all
+ replies = Reply.scoped(:where => [ "id IN (?)", [] ]).to_a
assert_equal 0, replies.size
end
@@ -1556,7 +1556,7 @@ def test_count_with_join
def test_no_limit_offset
assert_nothing_raised do
- Developer.scoped(:offset => 2).all
+ Developer.scoped(:offset => 2).to_a
end
end
@@ -1570,43 +1570,43 @@ def test_last
end
def test_all
- developers = Developer.all
+ developers = Developer.to_a
assert_kind_of Array, developers
- assert_equal Developer.all, developers
+ assert_equal Developer.to_a, developers
end
def test_all_with_conditions
- assert_equal Developer.scoped(:order => 'id desc').all, Developer.order('id desc').all
+ assert_equal Developer.scoped(:order => 'id desc').to_a, Developer.order('id desc').to_a
end
def test_find_ordered_last
last = Developer.scoped(:order => 'developers.salary ASC').last
- assert_equal last, Developer.scoped(:order => 'developers.salary ASC').all.last
+ assert_equal last, Developer.scoped(:order => 'developers.salary ASC').to_a.last
end
def test_find_reverse_ordered_last
last = Developer.scoped(:order => 'developers.salary DESC').last
- assert_equal last, Developer.scoped(:order => 'developers.salary DESC').all.last
+ assert_equal last, Developer.scoped(:order => 'developers.salary DESC').to_a.last
end
def test_find_multiple_ordered_last
last = Developer.scoped(:order => 'developers.name, developers.salary DESC').last
- assert_equal last, Developer.scoped(:order => 'developers.name, developers.salary DESC').all.last
+ assert_equal last, Developer.scoped(:order => 'developers.name, developers.salary DESC').to_a.last
end
def test_find_keeps_multiple_order_values
- combined = Developer.scoped(:order => 'developers.name, developers.salary').all
- assert_equal combined, Developer.scoped(:order => ['developers.name', 'developers.salary']).all
+ combined = Developer.scoped(:order => 'developers.name, developers.salary').to_a
+ assert_equal combined, Developer.scoped(:order => ['developers.name', 'developers.salary']).to_a
end
def test_find_keeps_multiple_group_values
- combined = Developer.scoped(:group => 'developers.name, developers.salary, developers.id, developers.created_at, developers.updated_at').all
- assert_equal combined, Developer.scoped(:group => ['developers.name', 'developers.salary', 'developers.id', 'developers.created_at', 'developers.updated_at']).all
+ combined = Developer.scoped(:group => 'developers.name, developers.salary, developers.id, developers.created_at, developers.updated_at').to_a
+ assert_equal combined, Developer.scoped(:group => ['developers.name', 'developers.salary', 'developers.id', 'developers.created_at', 'developers.updated_at']).to_a
end
def test_find_symbol_ordered_last
last = Developer.scoped(:order => :salary).last
- assert_equal last, Developer.scoped(:order => :salary).all.last
+ assert_equal last, Developer.scoped(:order => :salary).to_a.last
end
def test_abstract_class
@@ -1890,7 +1890,7 @@ def test_cache_key_format_for_existing_record_with_nil_updated_at
def test_uniq_delegates_to_scoped
scope = stub
- Bird.stubs(:scoped).returns(mock(:uniq => scope))
+ Bird.stubs(:all).returns(mock(:uniq => scope))
assert_equal scope, Bird.uniq
end
@@ -1957,7 +1957,7 @@ def test_default_values_are_deeply_dupped
scope.expects(meth).with(:foo, :bar).returns(record)
klass = Class.new(ActiveRecord::Base)
- klass.stubs(:scoped => scope)
+ klass.stubs(:all => scope)
assert_equal record, klass.public_send(meth, :foo, :bar)
end
@@ -1967,4 +1967,12 @@ def test_default_values_are_deeply_dupped
klass = Class.new(ActiveRecord::Base)
assert_equal ['foo'], klass.scoped(select: 'foo').select_values
end
+
+ test "Model.to_a returns an array" do
+ assert_equal Post.all.to_a, Post.to_a
+ end
+
+ test "Model.all returns a relation" do
+ assert Post.all.is_a?(ActiveRecord::Relation)
+ end
end
View
2  activerecord/test/cases/batches_test.rb
@@ -116,7 +116,7 @@ def test_find_in_batches_should_ignore_the_order_default_scope
end
def test_find_in_batches_should_not_ignore_the_default_scope_if_it_is_other_then_order
- special_posts_ids = SpecialPostWithDefaultScope.all.map(&:id).sort
+ special_posts_ids = SpecialPostWithDefaultScope.to_a.map(&:id).sort
posts = []
SpecialPostWithDefaultScope.find_in_batches do |batch|
posts.concat(batch)
View
2  activerecord/test/cases/calculations_test.rb
@@ -542,7 +542,7 @@ def test_pluck_with_selection_clause
end
def test_plucks_with_ids
- assert_equal Company.all.map(&:id).sort, Company.ids.sort
+ assert_equal Company.to_a.map(&:id).sort, Company.ids.sort
end
def test_pluck_not_auto_table_name_prefix_if_column_included
View
20 activerecord/test/cases/deprecated_dynamic_methods_test.rb
@@ -260,21 +260,21 @@ def test_find_last_by_two_attributes
def test_find_last_with_limit_gives_same_result_when_loaded_and_unloaded
scope = Topic.limit(2)
unloaded_last = scope.last
- loaded_last = scope.all.last
+ loaded_last = scope.to_a.last
assert_equal loaded_last, unloaded_last
end
def test_find_last_with_limit_and_offset_gives_same_result_when_loaded_and_unloaded
scope = Topic.offset(2).limit(2)
unloaded_last = scope.last
- loaded_last = scope.all.last
+ loaded_last = scope.to_a.last
assert_equal loaded_last, unloaded_last
end
def test_find_last_with_offset_gives_same_result_when_loaded_and_unloaded
scope = Topic.offset(3)
unloaded_last = scope.last
- loaded_last = scope.all.last
+ loaded_last = scope.to_a.last
assert_equal loaded_last, unloaded_last
end
@@ -292,17 +292,17 @@ def test_forwarding_to_dynamic_finders
end
def test_dynamic_find_all_should_respect_association_order
- assert_equal [companies(:second_client), companies(:first_client)], companies(:first_firm).clients_sorted_desc.scoped(:where => "type = 'Client'").all
+ assert_equal [companies(:second_client), companies(:first_client)], companies(:first_firm).clients_sorted_desc.scoped(:where => "type = 'Client'").to_a
assert_equal [companies(:second_client), companies(:first_client)], companies(:first_firm).clients_sorted_desc.find_all_by_type('Client')
end
def test_dynamic_find_all_should_respect_association_limit
- assert_equal 1, companies(:first_firm).limited_clients.scoped(:where => "type = 'Client'").all.length
+ assert_equal 1, companies(:first_firm).limited_clients.scoped(:where => "type = 'Client'").to_a.length
assert_equal 1, companies(:first_firm).limited_clients.find_all_by_type('Client').length
end
def test_dynamic_find_all_limit_should_override_association_limit
- assert_equal 2, companies(:first_firm).limited_clients.scoped(:where => "type = 'Client'", :limit => 9_000).all.length
+ assert_equal 2, companies(:first_firm).limited_clients.scoped(:where => "type = 'Client'", :limit => 9_000).to_a.length
assert_equal 2, companies(:first_firm).limited_clients.find_all_by_type('Client', :limit => 9_000).length
end
@@ -320,22 +320,22 @@ def test_dynamic_find_or_create_from_two_attributes_using_an_association
end
def test_dynamic_find_all_should_respect_association_order_for_through
- assert_equal [Comment.find(10), Comment.find(7), Comment.find(6), Comment.find(3)], authors(:david).comments_desc.scoped(:where => "comments.type = 'SpecialComment'").all
+ assert_equal [Comment.find(10), Comment.find(7), Comment.find(6), Comment.find(3)], authors(:david).comments_desc.scoped(:where => "comments.type = 'SpecialComment'").to_a
assert_equal [Comment.find(10), Comment.find(7), Comment.find(6), Comment.find(3)], authors(:david).comments_desc.find_all_by_type('SpecialComment')
end
def test_dynamic_find_all_should_respect_association_limit_for_through
- assert_equal 1, authors(:david).limited_comments.scoped(:where => "comments.type = 'SpecialComment'").all.length
+ assert_equal 1, authors(:david).limited_comments.scoped(:where => "comments.type = 'SpecialComment'").to_a.length
assert_equal 1, authors(:david).limited_comments.find_all_by_type('SpecialComment').length
end
def test_dynamic_find_all_order_should_override_association_limit_for_through
- assert_equal 4, authors(:david).limited_comments.scoped(:where => "comments.type = 'SpecialComment'", :limit => 9_000).all.length
+ assert_equal 4, authors(:david).limited_comments.scoped(:where => "comments.type = 'SpecialComment'", :limit => 9_000).to_a.length
assert_equal 4, authors(:david).limited_comments.find_all_by_type('SpecialComment', :limit => 9_000).length
end
def test_find_all_include_over_the_same_table_for_through
- assert_equal 2, people(:michael).posts.scoped(:includes => :people).all.length
+ assert_equal 2, people(:michael).posts.scoped(:includes => :people).to_a.length
end
def test_find_or_create_by_resets_cached_counters
View
8 activerecord/test/cases/explain_test.rb
@@ -20,7 +20,7 @@ def test_logging_query_plan_with_logger
end
with_threshold(0) do
- Car.where(:name => 'honda').all
+ Car.where(:name => 'honda').to_a
end
end
@@ -45,7 +45,7 @@ def test_collect_queries_for_explain
queries = Thread.current[:available_queries_for_explain] = []
with_threshold(0) do
- Car.where(:name => 'honda').all
+ Car.where(:name => 'honda').to_a
end
sql, binds = queries[0]
@@ -58,7 +58,7 @@ def test_collect_queries_for_explain
def test_collecting_queries_for_explain
result, queries = ActiveRecord::Base.collecting_queries_for_explain do
- Car.where(:name => 'honda').all
+ Car.where(:name => 'honda').to_a
end
sql, binds = queries[0]
@@ -112,7 +112,7 @@ def test_silence_auto_explain
base.expects(:collecting_sqls_for_explain).never
base.logger.expects(:warn).never
base.silence_auto_explain do
- with_threshold(0) { Car.all }
+ with_threshold(0) { Car.to_a }
end
end
View
34 activerecord/test/cases/finder_test.rb
@@ -105,7 +105,7 @@ def test_find_by_ids_with_limit_and_offset
# Also test an edge case: If you have 11 results, and you set a
# limit of 3 and offset of 9, then you should find that there
# will be only 2 results, regardless of the limit.
- devs = Developer.all
+ devs = Developer.to_a
last_devs = Developer.scoped(:limit => 3, :offset => 9).find devs.map(&:id)
assert_equal 2, last_devs.size
end
@@ -123,7 +123,7 @@ def test_find_by_ids_missing_one
end
def test_find_with_group_and_sanitized_having_method
- developers = Developer.group(:salary).having("sum(salary) > ?", 10000).select('salary').all
+ developers = Developer.group(:salary).having("sum(salary) > ?", 10000).select('salary').to_a
assert_equal 3, developers.size
assert_equal 3, developers.map(&:salary).uniq.size
assert developers.all? { |developer| developer.salary > 10000 }
@@ -306,23 +306,23 @@ def test_find_on_association_proxy_conditions
end
def test_find_on_hash_conditions_with_range
- assert_equal [1,2], Topic.scoped(:where => { :id => 1..2 }).all.map(&:id).sort
+ assert_equal [1,2], Topic.scoped(:where => { :id => 1..2 }).to_a.map(&:id).sort
assert_raise(ActiveRecord::RecordNotFound) { Topic.scoped(:where => { :id => 2..3 }).find(1) }
end
def test_find_on_hash_conditions_with_end_exclusive_range
- assert_equal [1,2,3], Topic.scoped(:where => { :id => 1..3 }).all.map(&:id).sort
- assert_equal [1,2], Topic.scoped(:where => { :id => 1...3 }).all.map(&:id).sort
+ assert_equal [1,2,3], Topic.scoped(:where => { :id => 1..3 }).to_a.map(&:id).sort
+ assert_equal [1,2], Topic.scoped(:where => { :id => 1...3 }).to_a.map(&:id).sort
assert_raise(ActiveRecord::RecordNotFound) { Topic.scoped(:where => { :id => 2...3 }).find(3) }
end
def test_find_on_hash_conditions_with_multiple_ranges
- assert_equal [1,2,3], Comment.scoped(:where => { :id => 1..3, :post_id => 1..2 }).all.map(&:id).sort
- assert_equal [1], Comment.scoped(:where => { :id => 1..1, :post_id => 1..10 }).all.map(&:id).sort
+ assert_equal [1,2,3], Comment.scoped(:where => { :id => 1..3, :post_id => 1..2 }).to_a.map(&:id).sort
+ assert_equal [1], Comment.scoped(:where => { :id => 1..1, :post_id => 1..10 }).to_a.map(&:id).sort
end
def test_find_on_hash_conditions_with_array_of_integers_and_ranges
- assert_equal [1,2,3,5,6,7,8,9], Comment.scoped(:where => {:id => [1..2, 3, 5, 6..8, 9]}).all.map(&:id).sort
+ assert_equal [1,2,3,5,6,7,8,9], Comment.scoped(:where => {:id => [1..2, 3, 5, 6..8, 9]}).to_a.map(&:id).sort
end
def test_find_on_multiple_hash_conditions
@@ -364,9 +364,9 @@ def test_hash_condition_find_with_escaped_characters
end
def test_hash_condition_find_with_array
- p1, p2 = Post.scoped(:limit => 2, :order => 'id asc').all
- assert_equal [p1, p2], Post.scoped(:where => { :id => [p1, p2] }, :order => 'id asc').all
- assert_equal [p1, p2], Post.scoped(:where => { :id => [p1, p2.id] }, :order => 'id asc').all
+ p1, p2 = Post.scoped(:limit => 2, :order => 'id asc').to_a
+ assert_equal [p1, p2], Post.scoped(:where => { :id => [p1, p2] }, :order => 'id asc').to_a
+ assert_equal [p1, p2], Post.scoped(:where => { :id => [p1, p2.id] }, :order => 'id asc').to_a
end
def test_hash_condition_find_with_nil
@@ -598,7 +598,7 @@ def test_find_all_with_join
developers_on_project_one = Developer.scoped(
:joins => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
:where => 'project_id=1'
- ).all
+ ).to_a
assert_equal 3, developers_on_project_one.length
developer_names = developers_on_project_one.map { |d| d.name }
assert developer_names.include?('David')
@@ -644,7 +644,7 @@ def test_find_by_empty_in_condition
end
def test_find_by_records
- p1, p2 = Post.scoped(:limit => 2, :order => 'id asc').all
+ p1, p2 = Post.scoped(:limit => 2, :order => 'id asc').to_a
assert_equal [p1, p2], Post.scoped(:where => ['id in (?)', [p1, p2]], :order => 'id asc')
assert_equal [p1, p2], Post.scoped(:where => ['id in (?)', [p1, p2.id]], :order => 'id asc')
end
@@ -673,10 +673,10 @@ def test_select_rows
end
def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
- assert_equal 2, Post.scoped(:includes => { :authors => :author_address }, :order => 'author_addresses.id DESC ', :limit => 2).all.size
+ assert_equal 2, Post.scoped(:includes => { :authors => :author_address }, :order => 'author_addresses.id DESC ', :limit => 2).to_a.size
assert_equal 3, Post.scoped(:includes => { :author => :author_address, :authors => :author_address},
- :order => 'author_addresses_authors.id DESC ', :limit => 3).all.size
+ :order => 'author_addresses_authors.id DESC ', :limit => 3).to_a.size
end
def test_find_with_nil_inside_set_passed_for_one_attribute
@@ -704,7 +704,7 @@ def test_with_limiting_with_custom_select
posts = Post.references(:authors).scoped(
:includes => :author, :select => ' posts.*, authors.id as "author_id"',
:limit => 3, :order => 'posts.id'
- ).all
+ ).to_a
assert_equal 3, posts.size
assert_equal [0, 1, 1], posts.map(&:author_id).sort
end
@@ -719,7 +719,7 @@ def test_find_one_message_with_custom_primary_key
end
def test_finder_with_offset_string
- assert_nothing_raised(ActiveRecord::StatementInvalid) { Topic.scoped(:offset => "3").all }
+ assert_nothing_raised(ActiveRecord::StatementInvalid) { Topic.scoped(:offset => "3").to_a }
end
protected
View
12 activerecord/test/cases/inheritance_test.rb
@@ -27,7 +27,7 @@ def read_attribute(name)
end
})
company.save!
- company = Company.all.find { |x| x.id == company.id }
+ company = Company.to_a.find { |x| x.id == company.id }
assert_equal ' ', company.type
end
@@ -128,7 +128,7 @@ def test_alt_inheritance_find
end
def test_inheritance_find_all
- companies = Company.scoped(:order => 'id').all
+ companies = Company.scoped(:order => 'id').to_a
assert_kind_of Firm, companies[0], "37signals should be a firm"
assert_kind_of Client, companies[1], "Summit should be a client"
end
@@ -179,9 +179,9 @@ def test_alt_finding_incorrect_type_data
def test_update_all_within_inheritance
Client.update_all "name = 'I am a client'"
- assert_equal "I am a client", Client.all.first.name
+ assert_equal "I am a client", Client.to_a.first.name
# Order by added as otherwise Oracle tests were failing because of different order of results
- assert_equal "37signals", Firm.scoped(:order => "id").all.first.name
+ assert_equal "37signals", Firm.scoped(:order => "id").to_a.first.name
end
def test_alt_update_all_within_inheritance
@@ -220,7 +220,7 @@ def test_complex_inheritance
assert_equal very_special_client, SpecialClient.scoped(:where => "name = 'veryspecial'").first
assert_equal very_special_client, Company.scoped(:where => "name = 'veryspecial'").first
assert_equal very_special_client, Client.scoped(:where => "name = 'veryspecial'").first
- assert_equal 1, Client.scoped(:where => "name = 'Summit'").all.size
+ assert_equal 1, Client.scoped(:where => "name = 'Summit'").to_a.size
assert_equal very_special_client, Client.find(very_special_client.id)
end
@@ -260,7 +260,7 @@ def test_inheritance_without_mapping
private
def switch_to_alt_inheritance_column
# we don't want misleading test results, so get rid of the values in the type column
- Company.scoped(:order => 'id').all.each do |c|
+ Company.scoped(:order => 'id').to_a.each do |c|
c['type'] = nil
c.save
end