2011-06-06 14:17:44 -04:00
require " cases/helper "
2008-01-18 02:30:42 -05:00
require 'models/post'
2008-01-18 23:19:53 -05:00
require 'models/tagging'
2008-12-18 14:07:55 -05:00
require 'models/tag'
2008-01-18 02:30:42 -05:00
require 'models/comment'
require 'models/author'
require 'models/category'
require 'models/company'
require 'models/person'
require 'models/reader'
2008-01-31 02:50:15 -05:00
require 'models/owner'
require 'models/pet'
2008-04-25 18:23:48 -04:00
require 'models/reference'
require 'models/job'
2008-05-01 00:30:50 -04:00
require 'models/subscriber'
require 'models/subscription'
require 'models/book'
2008-06-08 12:00:56 -04:00
require 'models/developer'
require 'models/project'
2009-01-09 15:13:17 -05:00
require 'models/member'
require 'models/membership'
require 'models/club'
2010-12-15 19:40:03 -05:00
require 'models/categorization'
2011-01-01 13:18:54 -05:00
require 'models/sponsor'
2005-04-03 13:50:11 -04:00
2008-01-21 12:20:51 -05:00
class EagerAssociationTest < ActiveRecord :: TestCase
2008-09-26 23:28:21 -04:00
fixtures :posts , :comments , :authors , :author_addresses , :categories , :categories_posts ,
2010-12-15 19:40:03 -05:00
:companies , :accounts , :tags , :taggings , :people , :readers , :categorizations ,
2008-06-08 12:00:56 -04:00
:owners , :pets , :author_favorites , :jobs , :references , :subscribers , :subscriptions , :books ,
2011-01-01 13:18:54 -05:00
:developers , :projects , :developers_projects , :members , :memberships , :clubs , :sponsors
2005-04-03 13:50:11 -04:00
2009-01-09 15:13:17 -05:00
def test_eager_with_has_one_through_join_model_with_conditions_on_the_through
2012-07-27 12:27:47 -04:00
member = Member . all . merge! ( :includes = > :favourite_club ) . find ( members ( :some_other_guy ) . id )
2009-01-09 15:13:17 -05:00
assert_nil member . favourite_club
end
2005-04-03 13:50:11 -04:00
def test_loading_with_one_association
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > :comments ) . to_a
2005-07-04 13:16:18 -04:00
post = posts . find { | p | p . id == 1 }
assert_equal 2 , post . comments . size
assert post . comments . include? ( comments ( :greetings ) )
2005-04-03 13:50:11 -04:00
2012-07-27 12:27:47 -04:00
post = Post . all . merge! ( :includes = > :comments , :where = > " posts.title = 'Welcome to the weblog' " ) . first
2005-04-03 13:50:11 -04:00
assert_equal 2 , post . comments . size
2005-06-10 09:54:58 -04:00
assert post . comments . include? ( comments ( :greetings ) )
2008-05-01 19:00:42 -04:00
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > :last_comment ) . to_a
2008-05-01 19:00:42 -04:00
post = posts . find { | p | p . id == 1 }
assert_equal Post . find ( 1 ) . last_comment , post . last_comment
2005-04-03 13:50:11 -04:00
end
2008-08-25 20:01:24 -04:00
def test_loading_with_one_association_with_non_preload
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > :last_comment , :order = > 'comments.id DESC' ) . to_a
2008-08-25 20:01:24 -04:00
post = posts . find { | p | p . id == 1 }
assert_equal Post . find ( 1 ) . last_comment , post . last_comment
end
2005-09-24 19:58:13 -04:00
def test_loading_conditions_with_or
2012-07-27 12:27:47 -04:00
posts = authors ( :david ) . posts . references ( :comments ) . merge (
2012-04-27 08:11:13 -04:00
:includes = > :comments ,
:where = > " comments.body like 'Normal%' OR comments. #{ QUOTED_TYPE } = 'SpecialComment' "
2012-07-27 07:01:25 -04:00
) . to_a
2005-09-24 19:58:13 -04:00
assert_nil posts . detect { | p | p . author_id != authors ( :david ) . id } ,
" expected to find only david's posts "
end
2005-04-19 12:32:57 -04:00
def test_with_ordering
2012-07-27 12:27:47 -04:00
list = Post . all . merge! ( :includes = > :comments , :order = > " posts.id DESC " ) . to_a
2010-10-18 19:27:40 -04:00
[ :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
2005-09-24 19:58:13 -04:00
] . each_with_index do | post , index |
assert_equal posts ( post ) , list [ index ]
end
2005-04-19 12:32:57 -04:00
end
2005-07-10 00:22:08 -04:00
2007-12-04 21:30:30 -05:00
def test_with_two_tables_in_from_without_getting_double_quoted
2009-12-27 17:26:20 -05:00
posts = Post . select ( " posts.* " ) . from ( " authors, posts " ) . eager_load ( :comments ) . where ( " posts.author_id = authors.id " ) . order ( " posts.id " ) . to_a
2007-12-04 21:30:30 -05:00
assert_equal 2 , posts . first . comments . size
end
2005-04-03 13:50:11 -04:00
def test_loading_with_multiple_associations
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > [ :comments , :author , :categories ] , :order = > " posts.id " ) . to_a
2005-04-03 13:50:11 -04:00
assert_equal 2 , posts . first . comments . size
2005-04-10 13:24:56 -04:00
assert_equal 2 , posts . first . categories . size
2005-06-10 09:54:58 -04:00
assert posts . first . comments . include? ( comments ( :greetings ) )
2005-04-03 13:50:11 -04:00
end
2008-01-18 23:19:53 -05:00
def test_duplicate_middle_objects
2012-07-27 12:27:47 -04:00
comments = Comment . all . merge! ( :where = > 'post_id = 1' , :includes = > [ :post = > :author ] ) . to_a
2008-01-18 23:19:53 -05:00
assert_no_queries do
comments . each { | comment | comment . post . author . name }
end
end
2010-11-08 15:30:27 -05:00
def test_preloading_has_many_in_multiple_queries_with_more_ids_than_database_can_handle
2010-11-22 17:10:19 -05:00
Post . connection . expects ( :in_clause_length ) . at_least_once . returns ( 5 )
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > :comments ) . to_a
2010-11-27 06:31:17 -05:00
assert_equal 11 , posts . size
2010-11-08 15:30:27 -05:00
end
def test_preloading_has_many_in_one_queries_when_database_has_no_limit_on_ids_it_can_handle
2010-11-22 17:10:19 -05:00
Post . connection . expects ( :in_clause_length ) . at_least_once . returns ( nil )
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > :comments ) . to_a
2010-11-27 06:31:17 -05:00
assert_equal 11 , posts . size
2010-11-08 15:30:27 -05:00
end
def test_preloading_habtm_in_multiple_queries_with_more_ids_than_database_can_handle
2010-11-22 17:10:19 -05:00
Post . connection . expects ( :in_clause_length ) . at_least_once . returns ( 5 )
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > :categories ) . to_a
2010-11-27 06:31:17 -05:00
assert_equal 11 , posts . size
2010-11-08 15:30:27 -05:00
end
def test_preloading_habtm_in_one_queries_when_database_has_no_limit_on_ids_it_can_handle
2010-11-22 17:10:19 -05:00
Post . connection . expects ( :in_clause_length ) . at_least_once . returns ( nil )
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > :categories ) . to_a
2010-11-27 06:31:17 -05:00
assert_equal 11 , posts . size
2010-11-08 15:30:27 -05:00
end
2010-11-18 13:01:21 -05:00
2010-11-08 15:30:27 -05:00
def test_load_associated_records_in_one_query_when_adapter_has_no_limit
2010-11-22 17:10:19 -05:00
Post . connection . expects ( :in_clause_length ) . at_least_once . returns ( nil )
2011-02-22 19:17:01 -05:00
post = posts ( :welcome )
assert_queries ( 2 ) do
Post . includes ( :comments ) . where ( :id = > post . id ) . to_a
2010-11-08 15:30:27 -05:00
end
end
def test_load_associated_records_in_several_queries_when_many_ids_passed
2011-02-22 19:17:01 -05:00
Post . connection . expects ( :in_clause_length ) . at_least_once . returns ( 1 )
post1 , post2 = posts ( :welcome ) , posts ( :thinking )
assert_queries ( 3 ) do
Post . includes ( :comments ) . where ( :id = > [ post1 . id , post2 . id ] ) . to_a
2010-11-08 15:30:27 -05:00
end
end
def test_load_associated_records_in_one_query_when_a_few_ids_passed
2011-02-22 19:17:01 -05:00
Post . connection . expects ( :in_clause_length ) . at_least_once . returns ( 3 )
post = posts ( :welcome )
assert_queries ( 2 ) do
Post . includes ( :comments ) . where ( :id = > post . id ) . to_a
2010-11-08 15:30:27 -05:00
end
end
2008-02-28 15:42:01 -05:00
def test_including_duplicate_objects_from_belongs_to
2008-02-28 16:01:04 -05:00
popular_post = Post . create! ( :title = > 'foo' , :body = > " I like cars! " )
2008-02-28 15:42:01 -05:00
comment = popular_post . comments . create! ( :body = > " lol " )
popular_post . readers . create! ( :person = > people ( :michael ) )
popular_post . readers . create! ( :person = > people ( :david ) )
2012-07-27 12:27:47 -04:00
readers = Reader . all . merge! ( :where = > [ " post_id = ? " , popular_post . id ] ,
2012-07-27 07:01:25 -04:00
:includes = > { :post = > :comments } ) . to_a
2008-02-28 15:42:01 -05:00
readers . each do | reader |
assert_equal [ comment ] , reader . post . comments
end
end
def test_including_duplicate_objects_from_has_many
2008-02-28 16:01:04 -05:00
car_post = Post . create! ( :title = > 'foo' , :body = > " I like cars! " )
2008-02-28 15:42:01 -05:00
car_post . categories << categories ( :general )
car_post . categories << categories ( :technology )
comment = car_post . comments . create! ( :body = > " hmm " )
2012-07-27 12:27:47 -04:00
categories = Category . all . merge! ( :where = > { 'posts.id' = > car_post . id } ,
2012-07-27 07:01:25 -04:00
:includes = > { :posts = > :comments } ) . to_a
2008-02-28 15:42:01 -05:00
categories . each do | category |
assert_equal [ comment ] , category . posts [ 0 ] . comments
end
end
2011-04-30 20:40:53 -04:00
2011-02-27 16:40:10 -05:00
def test_associations_loaded_for_all_records
post = Post . create! ( :title = > 'foo' , :body = > " I like cars! " )
2011-04-30 20:40:53 -04:00
SpecialComment . create! ( :body = > 'Come on!' , :post = > post )
2011-02-27 16:40:10 -05:00
first_category = Category . create! :name = > 'First!' , :posts = > [ post ]
second_category = Category . create! :name = > 'Second!' , :posts = > [ post ]
categories = Category . where ( :id = > [ first_category . id , second_category . id ] ) . includes ( :posts = > :special_comments )
assert_equal categories . map { | category | category . posts . first . special_comments . loaded? } , [ true , true ]
end
2008-02-28 15:42:01 -05:00
2008-09-26 23:28:21 -04:00
def test_finding_with_includes_on_has_many_association_with_same_include_includes_only_once
author_id = authors ( :david ) . id
2012-07-27 12:27:47 -04:00
author = assert_queries ( 3 ) { Author . all . merge! ( :includes = > { :posts_with_comments = > :comments } ) . find ( author_id ) } # find the author, then find the posts, then find the comments
2008-09-26 23:28:21 -04:00
author . posts_with_comments . each do | post_with_comments |
assert_equal post_with_comments . comments . length , post_with_comments . comments . count
2012-05-11 12:19:30 -04:00
assert_nil post_with_comments . comments . to_a . uniq!
2008-09-26 23:28:21 -04:00
end
end
def test_finding_with_includes_on_has_one_assocation_with_same_include_includes_only_once
author = authors ( :david )
post = author . post_about_thinking_with_last_comment
last_comment = post . last_comment
2012-07-27 12:27:47 -04:00
author = assert_queries ( 3 ) { Author . all . merge! ( :includes = > { :post_about_thinking_with_last_comment = > :last_comment } ) . find ( author . id ) } # find the author, then find the posts, then find the comments
2008-09-26 23:28:21 -04:00
assert_no_queries do
assert_equal post , author . post_about_thinking_with_last_comment
assert_equal last_comment , author . post_about_thinking_with_last_comment . last_comment
end
end
def test_finding_with_includes_on_belongs_to_association_with_same_include_includes_only_once
post = posts ( :welcome )
author = post . author
author_address = author . author_address
2012-07-27 12:27:47 -04:00
post = assert_queries ( 3 ) { Post . all . merge! ( :includes = > { :author_with_address = > :author_address } ) . find ( post . id ) } # find the post, then find the author, then find the address
2008-09-26 23:28:21 -04:00
assert_no_queries do
assert_equal author , post . author_with_address
assert_equal author_address , post . author_with_address . author_address
end
end
def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once
post = posts ( :welcome )
post . update_attributes! ( :author = > nil )
2012-07-27 12:27:47 -04:00
post = assert_queries ( 1 ) { Post . all . merge! ( :includes = > { :author_with_address = > :author_address } ) . find ( post . id ) } # find the post, then find the author which is null so no query for the author or address
2008-09-26 23:28:21 -04:00
assert_no_queries do
assert_equal nil , post . author_with_address
end
end
2011-01-03 08:38:40 -05:00
def test_finding_with_includes_on_null_belongs_to_polymorphic_association
sponsor = sponsors ( :moustache_club_sponsor_for_groucho )
sponsor . update_attributes! ( :sponsorable = > nil )
2012-07-27 12:27:47 -04:00
sponsor = assert_queries ( 1 ) { Sponsor . all . merge! ( :includes = > :sponsorable ) . find ( sponsor . id ) }
2011-01-03 08:38:40 -05:00
assert_no_queries do
assert_equal nil , sponsor . sponsorable
end
end
2005-04-03 13:50:11 -04:00
def test_loading_from_an_association
2012-07-27 12:27:47 -04:00
posts = authors ( :david ) . posts . merge ( :includes = > :comments , :order = > " posts.id " ) . to_a
2005-04-03 13:50:11 -04:00
assert_equal 2 , posts . first . comments . size
end
2008-09-24 00:44:56 -04:00
def test_loading_from_an_association_that_has_a_hash_of_conditions
assert_nothing_raised do
2012-07-27 12:27:47 -04:00
Author . all . merge! ( :includes = > :hello_posts_with_hash_conditions ) . to_a
2008-09-24 00:44:56 -04:00
end
2012-07-27 12:27:47 -04:00
assert ! Author . all . merge! ( :includes = > :hello_posts_with_hash_conditions ) . find ( authors ( :david ) . id ) . hello_posts . empty?
2008-09-24 00:44:56 -04:00
end
2005-04-14 03:49:13 -04:00
def test_loading_with_no_associations
2012-07-27 12:27:47 -04:00
assert_nil Post . all . merge! ( :includes = > :author ) . find ( posts ( :authorless ) . id ) . author
2005-04-14 03:49:13 -04:00
end
2008-02-17 23:49:56 -05:00
def test_nested_loading_with_no_associations
assert_nothing_raised do
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :includes = > { :author = > :author_addresss } ) . find ( posts ( :authorless ) . id )
2008-02-17 23:49:56 -05:00
end
end
2011-11-30 18:12:24 -05:00
def test_nested_loading_through_has_one_association
2012-07-27 12:27:47 -04:00
aa = AuthorAddress . all . merge! ( :includes = > { :author = > :posts } ) . find ( author_addresses ( :david_address ) . id )
2011-11-30 18:12:24 -05:00
assert_equal aa . author . posts . count , aa . author . posts . length
end
def test_nested_loading_through_has_one_association_with_order
2012-07-27 12:27:47 -04:00
aa = AuthorAddress . all . merge! ( :includes = > { :author = > :posts } , :order = > 'author_addresses.id' ) . find ( author_addresses ( :david_address ) . id )
2011-11-30 18:12:24 -05:00
assert_equal aa . author . posts . count , aa . author . posts . length
end
def test_nested_loading_through_has_one_association_with_order_on_association
2012-07-27 12:27:47 -04:00
aa = AuthorAddress . all . merge! ( :includes = > { :author = > :posts } , :order = > 'authors.id' ) . find ( author_addresses ( :david_address ) . id )
2011-11-30 18:12:24 -05:00
assert_equal aa . author . posts . count , aa . author . posts . length
end
def test_nested_loading_through_has_one_association_with_order_on_nested_association
2012-07-27 12:27:47 -04:00
aa = AuthorAddress . all . merge! ( :includes = > { :author = > :posts } , :order = > 'posts.id' ) . find ( author_addresses ( :david_address ) . id )
2011-11-30 18:12:24 -05:00
assert_equal aa . author . posts . count , aa . author . posts . length
end
def test_nested_loading_through_has_one_association_with_conditions
2012-07-27 12:27:47 -04:00
aa = AuthorAddress . references ( :author_addresses ) . merge (
2012-04-27 09:17:28 -04:00
:includes = > { :author = > :posts } ,
:where = > " author_addresses.id > 0 "
) . find author_addresses ( :david_address ) . id
2011-11-30 18:12:24 -05:00
assert_equal aa . author . posts . count , aa . author . posts . length
end
def test_nested_loading_through_has_one_association_with_conditions_on_association
2012-07-27 12:27:47 -04:00
aa = AuthorAddress . references ( :authors ) . merge (
2012-04-27 09:17:28 -04:00
:includes = > { :author = > :posts } ,
:where = > " authors.id > 0 "
) . find author_addresses ( :david_address ) . id
2011-11-30 18:12:24 -05:00
assert_equal aa . author . posts . count , aa . author . posts . length
end
def test_nested_loading_through_has_one_association_with_conditions_on_nested_association
2012-07-27 12:27:47 -04:00
aa = AuthorAddress . references ( :posts ) . merge (
2012-04-27 09:17:28 -04:00
:includes = > { :author = > :posts } ,
:where = > " posts.id > 0 "
) . find author_addresses ( :david_address ) . id
2011-11-30 18:12:24 -05:00
assert_equal aa . author . posts . count , aa . author . posts . length
end
2008-01-31 02:50:15 -05:00
def test_eager_association_loading_with_belongs_to_and_foreign_keys
2012-07-27 12:27:47 -04:00
pets = Pet . all . merge! ( :includes = > :owner ) . to_a
2008-01-31 02:50:15 -05:00
assert_equal 3 , pets . length
end
2005-04-03 13:50:11 -04:00
def test_eager_association_loading_with_belongs_to
2012-07-27 12:27:47 -04:00
comments = Comment . all . merge! ( :includes = > :post ) . to_a
2010-10-06 07:06:51 -04:00
assert_equal 11 , comments . length
2005-06-11 16:56:12 -04:00
titles = comments . map { | c | c . post . title }
assert titles . include? ( posts ( :welcome ) . title )
2005-07-03 04:21:22 -04:00
assert titles . include? ( posts ( :sti_post_and_comments ) . title )
2005-04-03 13:50:11 -04:00
end
2008-01-18 02:30:42 -05:00
2005-07-11 02:09:08 -04:00
def test_eager_association_loading_with_belongs_to_and_limit
2012-07-27 12:27:47 -04:00
comments = Comment . all . merge! ( :includes = > :post , :limit = > 5 , :order = > 'comments.id' ) . to_a
2005-07-11 02:09:08 -04:00
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
2012-07-27 12:27:47 -04:00
comments = Comment . all . merge! ( :includes = > :post , :where = > 'post_id = 4' , :limit = > 3 , :order = > 'comments.id' ) . to_a
2005-07-11 02:09:08 -04:00
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
2012-07-27 12:27:47 -04:00
comments = Comment . all . merge! ( :includes = > :post , :limit = > 3 , :offset = > 2 , :order = > 'comments.id' ) . to_a
2005-07-11 02:09:08 -04:00
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
2012-07-27 12:27:47 -04:00
comments = Comment . all . merge! ( :includes = > :post , :where = > 'post_id = 4' , :limit = > 3 , :offset = > 1 , :order = > 'comments.id' ) . to_a
2005-07-11 02:09:08 -04:00
assert_equal 3 , comments . length
assert_equal [ 6 , 7 , 8 ] , comments . collect { | c | c . id }
end
2008-01-18 02:30:42 -05:00
2005-12-07 23:44:54 -05:00
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
2012-07-27 12:27:47 -04:00
comments = Comment . all . merge! ( :includes = > :post , :where = > [ 'post_id = ?' , 4 ] , :limit = > 3 , :offset = > 1 , :order = > 'comments.id' ) . to_a
2005-12-07 23:44:54 -05:00
assert_equal 3 , comments . length
assert_equal [ 6 , 7 , 8 ] , comments . collect { | c | c . id }
end
2008-02-10 21:50:58 -05:00
def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name
assert_nothing_raised do
2012-01-16 16:14:34 -05:00
ActiveSupport :: Deprecation . silence do
2012-07-27 12:27:47 -04:00
Comment . all . merge! ( :includes = > :post , :where = > [ 'posts.id = ?' , 4 ] ) . to_a
2012-01-16 16:14:34 -05:00
end
2008-02-10 21:50:58 -05:00
end
end
2009-05-04 10:49:43 -04:00
def test_eager_association_loading_with_belongs_to_and_conditions_hash
comments = [ ]
assert_nothing_raised do
2012-07-27 12:27:47 -04:00
comments = Comment . all . merge! ( :includes = > :post , :where = > { :posts = > { :id = > 4 } } , :limit = > 3 , :order = > 'comments.id' ) . to_a
2009-05-04 10:49:43 -04:00
end
assert_equal 3 , comments . length
assert_equal [ 5 , 6 , 7 ] , comments . collect { | c | c . id }
assert_no_queries do
comments . first . post
end
end
2008-02-10 21:50:58 -05:00
def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
2008-02-11 02:24:25 -05:00
quoted_posts_id = Comment . connection . quote_table_name ( 'posts' ) + '.' + Comment . connection . quote_column_name ( 'id' )
2008-02-10 21:50:58 -05:00
assert_nothing_raised do
2012-01-16 16:14:34 -05:00
ActiveSupport :: Deprecation . silence do
2012-07-27 12:27:47 -04:00
Comment . all . merge! ( :includes = > :post , :where = > [ " #{ quoted_posts_id } = ? " , 4 ] ) . to_a
2012-01-16 16:14:34 -05:00
end
2008-02-10 21:50:58 -05:00
end
end
def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
assert_nothing_raised do
2012-07-27 12:27:47 -04:00
Comment . all . merge! ( :includes = > :post , :order = > 'posts.id' ) . to_a
2008-02-10 21:50:58 -05:00
end
end
def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
2008-02-11 02:24:25 -05:00
quoted_posts_id = Comment . connection . quote_table_name ( 'posts' ) + '.' + Comment . connection . quote_column_name ( 'id' )
2008-02-10 21:50:58 -05:00
assert_nothing_raised do
2012-01-16 16:14:34 -05:00
ActiveSupport :: Deprecation . silence do
2012-07-27 12:27:47 -04:00
Comment . all . merge! ( :includes = > :post , :order = > quoted_posts_id ) . to_a
2012-01-16 16:14:34 -05:00
end
2008-02-10 21:50:58 -05:00
end
end
2005-07-11 02:09:08 -04:00
def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > [ :author , :very_special_comment ] , :limit = > 1 , :order = > 'posts.id' ) . to_a
2005-07-11 02:09:08 -04:00
assert_equal 1 , posts . length
2006-03-05 13:43:56 -05:00
assert_equal [ 1 ] , posts . collect { | p | p . id }
2005-07-11 02:09:08 -04:00
end
2008-01-18 02:30:42 -05:00
2005-07-11 02:09:08 -04:00
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > [ :author , :very_special_comment ] , :limit = > 1 , :offset = > 1 , :order = > 'posts.id' ) . to_a
2006-03-04 18:33:10 -05:00
assert_equal 1 , posts . length
2006-03-05 13:43:56 -05:00
assert_equal [ 2 ] , posts . collect { | p | p . id }
2005-07-11 02:09:08 -04:00
end
2008-01-18 02:30:42 -05:00
2007-12-20 20:49:01 -05:00
def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name
2012-07-27 12:27:47 -04:00
author_favorite = AuthorFavorite . all . merge! ( :includes = > :favorite_author ) . first
2007-12-20 20:49:01 -05:00
assert_equal authors ( :mary ) , assert_no_queries { author_favorite . favorite_author }
end
2008-04-25 18:23:48 -04:00
def test_eager_load_belongs_to_quotes_table_and_column_names
2012-04-27 09:17:28 -04:00
job = Job . includes ( :ideal_reference ) . find jobs ( :unicyclist ) . id
2008-04-25 18:23:48 -04:00
references ( :michael_unicyclist )
assert_no_queries { assert_equal references ( :michael_unicyclist ) , job . ideal_reference }
end
def test_eager_load_has_one_quotes_table_and_column_names
2012-07-27 12:27:47 -04:00
michael = Person . all . merge! ( :includes = > :favourite_reference ) . find ( people ( :michael ) )
2008-04-25 18:23:48 -04:00
references ( :michael_unicyclist )
assert_no_queries { assert_equal references ( :michael_unicyclist ) , michael . favourite_reference }
end
def test_eager_load_has_many_quotes_table_and_column_names
2012-07-27 12:27:47 -04:00
michael = Person . all . merge! ( :includes = > :references ) . find ( people ( :michael ) )
2008-04-25 18:23:48 -04:00
references ( :michael_magician , :michael_unicyclist )
assert_no_queries { assert_equal references ( :michael_magician , :michael_unicyclist ) , michael . references . sort_by ( & :id ) }
end
def test_eager_load_has_many_through_quotes_table_and_column_names
2012-07-27 12:27:47 -04:00
michael = Person . all . merge! ( :includes = > :jobs ) . find ( people ( :michael ) )
2008-04-25 18:23:48 -04:00
jobs ( :magician , :unicyclist )
assert_no_queries { assert_equal jobs ( :unicyclist , :magician ) , michael . jobs . sort_by ( & :id ) }
end
2008-05-01 00:30:50 -04:00
def test_eager_load_has_many_with_string_keys
subscriptions = subscriptions ( :webster_awdr , :webster_rfr )
2012-07-27 12:27:47 -04:00
subscriber = Subscriber . all . merge! ( :includes = > :subscriptions ) . find ( subscribers ( :second ) . id )
2008-05-01 00:30:50 -04:00
assert_equal subscriptions , subscriber . subscriptions . sort_by ( & :id )
end
2009-08-18 06:50:11 -04:00
2011-05-26 19:10:57 -04:00
def test_string_id_column_joins
s = Subscriber . create! do | c |
c . id = " PL "
end
2011-05-27 14:44:00 -04:00
b = Book . create!
2011-05-26 19:10:57 -04:00
2011-05-27 14:44:00 -04:00
Subscription . create! ( :subscriber_id = > " PL " , :book_id = > b . id )
2011-05-26 19:10:57 -04:00
s . reload
s . book_ids = s . book_ids
end
2008-05-01 00:30:50 -04:00
def test_eager_load_has_many_through_with_string_keys
books = books ( :awdr , :rfr )
2012-07-27 12:27:47 -04:00
subscriber = Subscriber . all . merge! ( :includes = > :books ) . find ( subscribers ( :second ) . id )
2008-05-01 00:30:50 -04:00
assert_equal books , subscriber . books . sort_by ( & :id )
end
2009-08-18 06:50:11 -04:00
2008-05-01 00:30:50 -04:00
def test_eager_load_belongs_to_with_string_keys
subscriber = subscribers ( :second )
2012-07-27 12:27:47 -04:00
subscription = Subscription . all . merge! ( :includes = > :subscriber ) . find ( subscriptions ( :webster_awdr ) . id )
2008-05-01 00:30:50 -04:00
assert_equal subscriber , subscription . subscriber
end
2007-09-15 19:50:12 -04:00
def test_eager_association_loading_with_explicit_join
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > :comments , :joins = > " INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary' " , :limit = > 1 , :order = > 'author_id' ) . to_a
2007-09-15 19:50:12 -04:00
assert_equal 1 , posts . length
end
2008-01-18 02:30:42 -05:00
2006-02-10 00:19:41 -05:00
def test_eager_with_has_many_through
2012-07-27 12:27:47 -04:00
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
2006-02-10 00:19:41 -05:00
assert_equal 2 , posts_with_comments . inject ( 0 ) { | sum , post | sum += post . comments . size }
2006-03-05 13:43:56 -05:00
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 }
2006-02-10 00:19:41 -05:00
end
2008-10-04 10:42:36 -04:00
def test_eager_with_has_many_through_a_belongs_to_association
author = authors ( :mary )
2010-11-15 13:12:09 -05:00
Post . create! ( :author = > author , :title = > " TITLE " , :body = > " BODY " )
2008-10-04 10:42:36 -04:00
author . author_favorites . create ( :favorite_author_id = > 1 )
author . author_favorites . create ( :favorite_author_id = > 2 )
2012-07-27 12:27:47 -04:00
posts_with_author_favorites = author . posts . merge ( :includes = > :author_favorites ) . to_a
2008-10-04 10:42:36 -04:00
assert_no_queries { posts_with_author_favorites . first . author_favorites . first . author_id }
end
2006-02-10 00:19:41 -05:00
2006-10-15 12:37:11 -04:00
def test_eager_with_has_many_through_an_sti_join_model
2012-07-27 12:27:47 -04:00
author = Author . all . merge! ( :includes = > :special_post_comments , :order = > 'authors.id' ) . first
2006-10-15 12:37:11 -04:00
assert_equal [ comments ( :does_it_hurt ) ] , assert_no_queries { author . special_post_comments }
end
2008-01-18 02:30:42 -05:00
2006-10-15 12:37:11 -04:00
def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
2012-07-27 12:27:47 -04:00
author = Author . all . merge! ( :includes = > :special_nonexistant_post_comments , :order = > 'authors.id' ) . first
2006-10-15 12:37:11 -04:00
assert_equal [ ] , author . special_nonexistant_post_comments
end
2006-11-01 15:28:48 -05:00
2006-10-15 12:37:11 -04:00
def test_eager_with_has_many_through_join_model_with_conditions
2012-07-27 12:27:47 -04:00
assert_equal Author . all . merge! ( :includes = > :hello_post_comments ,
2012-04-27 06:42:22 -04:00
:order = > 'authors.id' ) . first . hello_post_comments . sort_by ( & :id ) ,
2012-07-27 12:27:47 -04:00
Author . all . merge! ( :order = > 'authors.id' ) . first . hello_post_comments . sort_by ( & :id )
2006-10-15 12:37:11 -04:00
end
2008-05-07 16:35:41 -04:00
def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
2012-07-27 12:27:47 -04:00
assert_equal comments ( :more_greetings ) , Author . all . merge! ( :includes = > :comments_with_order_and_conditions ) . find ( authors ( :david ) . id ) . comments_with_order_and_conditions . first
2008-05-07 16:35:41 -04:00
end
def test_eager_with_has_many_through_join_model_with_include
2012-07-27 12:27:47 -04:00
author_comments = Author . all . merge! ( :includes = > :comments_with_include ) . find ( authors ( :david ) . id ) . comments_with_include . to_a
2008-05-07 16:35:41 -04:00
assert_no_queries do
author_comments . first . post . title
end
end
2010-06-28 16:37:38 -04:00
def test_eager_with_has_many_through_with_conditions_join_model_with_include
post_tags = Post . find ( posts ( :welcome ) . id ) . misc_tags
2012-07-27 12:27:47 -04:00
eager_post_tags = Post . all . merge! ( :includes = > :misc_tags ) . find ( 1 ) . misc_tags
2010-06-28 16:37:38 -04:00
assert_equal post_tags , eager_post_tags
end
2011-05-24 02:21:32 -04:00
def test_eager_with_has_many_through_join_model_ignores_default_includes
assert_nothing_raised do
authors ( :david ) . comments_on_posts_with_default_include . to_a
end
end
2005-10-18 08:02:25 -04:00
def test_eager_with_has_many_and_limit
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :order = > 'posts.id asc' , :includes = > [ :author , :comments ] , :limit = > 2 ) . to_a
2005-10-18 08:02:25 -04:00
assert_equal 2 , posts . size
assert_equal 3 , posts . inject ( 0 ) { | sum , post | sum += post . comments . size }
end
2005-12-07 23:44:54 -05:00
def test_eager_with_has_many_and_limit_and_conditions
2007-09-13 19:21:14 -04:00
if current_adapter? ( :OpenBaseAdapter )
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > [ :author , :comments ] , :limit = > 2 , :where = > " FETCHBLOB(posts.body) = 'hello' " , :order = > " posts.id " ) . to_a
2007-09-13 19:21:14 -04:00
else
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > [ :author , :comments ] , :limit = > 2 , :where = > " posts.body = 'hello' " , :order = > " posts.id " ) . to_a
2007-09-13 19:21:14 -04:00
end
2005-12-07 23:44:54 -05:00
assert_equal 2 , posts . size
assert_equal [ 4 , 5 ] , posts . collect { | p | p . id }
end
def test_eager_with_has_many_and_limit_and_conditions_array
2007-09-13 19:21:14 -04:00
if current_adapter? ( :OpenBaseAdapter )
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > [ :author , :comments ] , :limit = > 2 , :where = > [ " FETCHBLOB(posts.body) = ? " , 'hello' ] , :order = > " posts.id " ) . to_a
2007-09-13 19:21:14 -04:00
else
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > [ :author , :comments ] , :limit = > 2 , :where = > [ " posts.body = ? " , 'hello' ] , :order = > " posts.id " ) . to_a
2007-09-13 19:21:14 -04:00
end
2005-12-07 23:44:54 -05:00
assert_equal 2 , posts . size
2008-01-18 02:30:42 -05:00
assert_equal [ 4 , 5 ] , posts . collect { | p | p . id }
2005-12-07 23:44:54 -05:00
end
def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
2012-01-16 16:14:34 -05:00
posts = ActiveSupport :: Deprecation . silence do
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :includes = > [ :author , :comments ] , :limit = > 2 , :where = > [ " authors.name = ? " , 'David' ] ) . to_a
2012-01-16 16:14:34 -05:00
end
2006-02-09 04:17:40 -05:00
assert_equal 2 , posts . size
2008-01-18 02:30:42 -05:00
2012-01-16 16:14:34 -05:00
count = ActiveSupport :: Deprecation . silence do
Post . count ( :include = > [ :author , :comments ] , :limit = > 2 , :conditions = > [ " authors.name = ? " , 'David' ] )
end
2006-02-09 04:17:40 -05:00
assert_equal count , posts . size
2005-12-07 23:44:54 -05:00
end
2008-11-26 09:21:12 -05:00
def test_eager_with_has_many_and_limit_and_high_offset
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > [ :author , :comments ] , :limit = > 2 , :offset = > 10 , :where = > { 'authors.name' = > 'David' } ) . to_a
2006-04-06 00:16:08 -04:00
assert_equal 0 , posts . size
end
2008-11-26 09:21:12 -05:00
def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions
assert_queries ( 1 ) do
2012-04-13 13:26:04 -04:00
posts = Post . references ( :authors , :comments ) .
2012-07-27 12:27:47 -04:00
merge ( :includes = > [ :author , :comments ] , :limit = > 2 , :offset = > 10 ,
2012-07-27 07:01:25 -04:00
:where = > [ " authors.name = ? and comments.body = ? " , 'David' , 'go crazy' ] ) . to_a
2008-11-26 09:21:12 -05:00
assert_equal 0 , posts . size
end
end
def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash_conditions
assert_queries ( 1 ) do
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > [ :author , :comments ] , :limit = > 2 , :offset = > 10 ,
2012-07-27 07:01:25 -04:00
:where = > { 'authors.name' = > 'David' , 'comments.body' = > 'go crazy' } ) . to_a
2008-11-26 09:21:12 -05:00
assert_equal 0 , posts . size
end
end
def test_count_eager_with_has_many_and_limit_and_high_offset
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > [ :author , :comments ] , :limit = > 2 , :offset = > 10 , :where = > { 'authors.name' = > 'David' } ) . count ( :all )
2006-04-06 00:16:08 -04:00
assert_equal 0 , posts
end
2005-10-18 08:02:25 -04:00
def test_eager_with_has_many_and_limit_with_no_results
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > [ :author , :comments ] , :limit = > 2 , :where = > " posts.title = 'magic forest' " ) . to_a
2005-10-18 08:02:25 -04:00
assert_equal 0 , posts . size
end
2008-01-18 02:30:42 -05:00
2007-08-16 00:37:31 -04:00
def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
author = authors ( :david )
author_posts_without_comments = author . posts . select { | post | post . comments . blank? }
2012-04-13 13:26:04 -04:00
assert_equal author_posts_without_comments . size , author . posts . includes ( :comments ) . where ( 'comments.id is null' ) . references ( :comments ) . count
2007-08-16 00:37:31 -04:00
end
2009-08-18 06:50:11 -04:00
2008-02-17 19:14:54 -05:00
def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
person = people ( :michael )
person_posts_without_comments = person . posts . select { | post | post . comments . blank? }
assert_equal person_posts_without_comments . size , person . posts_with_no_comments . count
end
2005-10-18 08:02:25 -04:00
def test_eager_with_has_and_belongs_to_many_and_limit
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > :categories , :order = > " posts.id " , :limit = > 3 ) . to_a
2005-10-18 08:02:25 -04:00
assert_equal 3 , posts . size
assert_equal 2 , posts [ 0 ] . categories . size
assert_equal 1 , posts [ 1 ] . categories . size
assert_equal 0 , posts [ 2 ] . categories . size
assert posts [ 0 ] . categories . include? ( categories ( :technology ) )
assert posts [ 1 ] . categories . include? ( categories ( :general ) )
end
2012-03-01 22:10:06 -05:00
# Since the preloader for habtm gets raw row hashes from the database and then
# instantiates them, this test ensures that it only instantiates one actual
# object per record from the database.
2011-03-04 17:29:40 -05:00
def test_has_and_belongs_to_many_should_not_instantiate_same_records_multiple_times
welcome = posts ( :welcome )
categories = Category . includes ( :posts )
general = categories . find { | c | c == categories ( :general ) }
technology = categories . find { | c | c == categories ( :technology ) }
post1 = general . posts . to_a . find { | p | p == posts ( :welcome ) }
post2 = technology . posts . to_a . find { | p | p == posts ( :welcome ) }
assert_equal post1 . object_id , post2 . object_id
end
2005-10-18 08:02:25 -04:00
def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
2012-04-13 13:26:04 -04:00
posts =
authors ( :david ) . posts
. includes ( :comments )
. where ( " comments.body like 'Normal%' OR comments. #{ QUOTED_TYPE } = 'SpecialComment' " )
. references ( :comments )
. limit ( 2 )
. to_a
2006-02-09 04:17:40 -05:00
assert_equal 2 , posts . size
2008-01-18 02:30:42 -05:00
2012-04-13 13:26:04 -04:00
count =
Post . includes ( :comments , :author )
. where ( " authors.name = 'David' AND (comments.body like 'Normal%' OR comments. #{ QUOTED_TYPE } = 'SpecialComment') " )
. references ( :authors , :comments )
. limit ( 2 )
. count
2006-02-09 04:17:40 -05:00
assert_equal count , posts . size
2005-07-11 02:09:08 -04:00
end
2005-04-10 13:24:56 -04:00
2006-04-19 10:50:10 -04:00
def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
posts = nil
2012-04-13 13:26:04 -04:00
Post . includes ( :comments )
. where ( " comments.body like 'Normal%' OR comments. #{ QUOTED_TYPE } = 'SpecialComment' " )
. references ( :comments )
. scoping do
posts = authors ( :david ) . posts . limit ( 2 ) . to_a
2006-04-19 10:50:10 -04:00
assert_equal 2 , posts . size
end
2008-01-18 02:30:42 -05:00
2012-04-13 13:26:04 -04:00
Post . includes ( :comments , :author )
. where ( " authors.name = 'David' AND (comments.body like 'Normal%' OR comments. #{ QUOTED_TYPE } = 'SpecialComment') " )
. references ( :authors , :comments )
. scoping do
count = Post . limit ( 2 ) . count
2006-04-19 10:50:10 -04:00
assert_equal count , posts . size
end
end
2005-04-10 13:24:56 -04:00
def test_eager_association_loading_with_habtm
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > :categories , :order = > " posts.id " ) . to_a
2005-04-18 01:55:20 -04:00
assert_equal 2 , posts [ 0 ] . categories . size
assert_equal 1 , posts [ 1 ] . categories . size
assert_equal 0 , posts [ 2 ] . categories . size
2005-06-10 09:54:58 -04:00
assert posts [ 0 ] . categories . include? ( categories ( :technology ) )
assert posts [ 1 ] . categories . include? ( categories ( :general ) )
2005-04-10 13:24:56 -04:00
end
2005-07-10 00:22:08 -04:00
2005-04-10 11:49:49 -04:00
def test_eager_with_inheritance
2012-07-27 12:27:47 -04:00
SpecialPost . all . merge! ( :includes = > [ :comments ] ) . to_a
2005-07-10 00:22:08 -04:00
end
2005-06-10 09:54:58 -04:00
2005-07-03 04:21:22 -04:00
def test_eager_has_one_with_association_inheritance
2012-07-27 12:27:47 -04:00
post = Post . all . merge! ( :includes = > [ :very_special_comment ] ) . find ( 4 )
2005-07-03 04:21:22 -04:00
assert_equal " VerySpecialComment " , post . very_special_comment . class . to_s
2005-07-10 00:22:08 -04:00
end
2005-07-03 04:21:22 -04:00
def test_eager_has_many_with_association_inheritance
2012-07-27 12:27:47 -04:00
post = Post . all . merge! ( :includes = > [ :special_comments ] ) . find ( 4 )
2005-07-03 04:21:22 -04:00
post . special_comments . each do | special_comment |
2010-10-06 07:06:51 -04:00
assert special_comment . is_a? ( SpecialComment )
2005-07-03 04:21:22 -04:00
end
2005-07-10 00:22:08 -04:00
end
2005-07-03 04:21:22 -04:00
def test_eager_habtm_with_association_inheritance
2012-07-27 12:27:47 -04:00
post = Post . all . merge! ( :includes = > [ :special_categories ] ) . find ( 6 )
2005-07-03 04:21:22 -04:00
assert_equal 1 , post . special_categories . size
post . special_categories . each do | special_category |
assert_equal " SpecialCategory " , special_category . class . to_s
end
end
2005-06-10 09:54:58 -04:00
def test_eager_with_has_one_dependent_does_not_destroy_dependent
assert_not_nil companies ( :first_firm ) . account
2012-07-27 12:27:47 -04:00
f = Firm . all . merge! ( :includes = > :account ,
2012-04-27 08:11:13 -04:00
:where = > [ " companies.name = ? " , " 37signals " ] ) . first
2005-07-10 00:22:08 -04:00
assert_not_nil f . account
assert_equal companies ( :first_firm , :reload ) . account , f . account
2005-06-10 09:54:58 -04:00
end
2008-01-18 02:30:42 -05:00
2007-08-16 00:36:55 -04:00
def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
author = authors ( :david )
posts_with_no_comments = author . posts . select { | post | post . comments . blank? }
assert_equal posts_with_no_comments . size , author . posts_with_no_comments . size
assert_equal posts_with_no_comments , author . posts_with_no_comments
end
2005-07-09 13:14:14 -04:00
def test_eager_with_invalid_association_reference
2009-03-08 16:11:58 -04:00
assert_raise ( ActiveRecord :: ConfigurationError , " Association was not found; perhaps you misspelled it? You specified :include => :monkeys " ) {
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :includes = > :monkeys ) . find ( 6 )
2005-09-18 16:41:44 -04:00
}
2009-03-08 16:11:58 -04:00
assert_raise ( ActiveRecord :: ConfigurationError , " Association was not found; perhaps you misspelled it? You specified :include => :monkeys " ) {
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :includes = > [ :monkeys ] ) . find ( 6 )
2005-07-09 13:14:14 -04:00
}
2009-03-08 16:11:58 -04:00
assert_raise ( ActiveRecord :: ConfigurationError , " Association was not found; perhaps you misspelled it? You specified :include => :monkeys " ) {
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :includes = > [ 'monkeys' ] ) . find ( 6 )
2005-09-18 16:41:44 -04:00
}
2009-03-08 16:11:58 -04:00
assert_raise ( ActiveRecord :: ConfigurationError , " Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants " ) {
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :includes = > [ :monkeys , :elephants ] ) . find ( 6 )
2005-07-09 13:14:14 -04:00
}
end
2008-01-18 02:30:42 -05:00
2011-05-23 22:05:06 -04:00
def test_eager_with_default_scope
developer = EagerDeveloperWithDefaultScope . where ( :name = > 'David' ) . first
2012-07-27 07:01:25 -04:00
projects = Project . order ( :id ) . to_a
2011-05-23 22:05:06 -04:00
assert_no_queries do
assert_equal ( projects , developer . projects )
end
end
def test_eager_with_default_scope_as_class_method
developer = EagerDeveloperWithClassMethodDefaultScope . where ( :name = > 'David' ) . first
2012-07-27 07:01:25 -04:00
projects = Project . order ( :id ) . to_a
2011-05-23 22:05:06 -04:00
assert_no_queries do
assert_equal ( projects , developer . projects )
end
end
def test_eager_with_default_scope_as_lambda
developer = EagerDeveloperWithLambdaDefaultScope . where ( :name = > 'David' ) . first
2012-07-27 07:01:25 -04:00
projects = Project . order ( :id ) . to_a
2011-05-23 22:05:06 -04:00
assert_no_queries do
assert_equal ( projects , developer . projects )
end
end
def test_eager_with_default_scope_as_block
developer = EagerDeveloperWithBlockDefaultScope . where ( :name = > 'David' ) . first
2012-07-27 07:01:25 -04:00
projects = Project . order ( :id ) . to_a
2011-05-23 22:05:06 -04:00
assert_no_queries do
assert_equal ( projects , developer . projects )
end
end
def test_eager_with_default_scope_as_callable
developer = EagerDeveloperWithCallableDefaultScope . where ( :name = > 'David' ) . first
2012-07-27 07:01:25 -04:00
projects = Project . order ( :id ) . to_a
2011-05-23 22:05:06 -04:00
assert_no_queries do
assert_equal ( projects , developer . projects )
end
end
2006-03-16 01:17:54 -05:00
def find_all_ordered ( className , include = nil )
2012-07-27 12:27:47 -04:00
className . all . merge! ( :order = > " #{ className . table_name } . #{ className . primary_key } " , :includes = > include ) . to_a
2006-03-16 01:17:54 -05:00
end
2008-01-18 02:30:42 -05:00
2007-01-12 00:14:55 -05:00
def test_limited_eager_with_order
2012-01-16 16:14:34 -05:00
assert_equal (
posts ( :thinking , :sti_comments ) ,
2012-07-27 12:27:47 -04:00
Post . all . merge! (
2012-04-27 08:11:13 -04:00
:includes = > [ :author , :comments ] , :where = > { 'authors.name' = > 'David' } ,
2012-01-16 16:14:34 -05:00
:order = > 'UPPER(posts.title)' , :limit = > 2 , :offset = > 1
2012-07-27 07:01:25 -04:00
) . to_a
2012-01-16 16:14:34 -05:00
)
assert_equal (
posts ( :sti_post_and_comments , :sti_comments ) ,
2012-07-27 12:27:47 -04:00
Post . all . merge! (
2012-04-27 08:11:13 -04:00
:includes = > [ :author , :comments ] , :where = > { 'authors.name' = > 'David' } ,
2012-01-16 16:14:34 -05:00
:order = > 'UPPER(posts.title) DESC' , :limit = > 2 , :offset = > 1
2012-07-27 07:01:25 -04:00
) . to_a
2012-01-16 16:14:34 -05:00
)
2007-01-12 00:14:55 -05:00
end
2008-01-18 02:30:42 -05:00
2007-01-12 00:14:55 -05:00
def test_limited_eager_with_multiple_order_columns
2012-01-16 16:14:34 -05:00
assert_equal (
posts ( :thinking , :sti_comments ) ,
2012-07-27 12:27:47 -04:00
Post . all . merge! (
2012-04-27 08:11:13 -04:00
:includes = > [ :author , :comments ] , :where = > { 'authors.name' = > 'David' } ,
2012-01-16 16:14:34 -05:00
:order = > [ 'UPPER(posts.title)' , 'posts.id' ] , :limit = > 2 , :offset = > 1
2012-07-27 07:01:25 -04:00
) . to_a
2012-01-16 16:14:34 -05:00
)
assert_equal (
posts ( :sti_post_and_comments , :sti_comments ) ,
2012-07-27 12:27:47 -04:00
Post . all . merge! (
2012-04-27 08:11:13 -04:00
:includes = > [ :author , :comments ] , :where = > { 'authors.name' = > 'David' } ,
2012-01-16 16:14:34 -05:00
:order = > [ 'UPPER(posts.title) DESC' , 'posts.id' ] , :limit = > 2 , :offset = > 1
2012-07-27 07:01:25 -04:00
) . to_a
2012-01-16 16:14:34 -05:00
)
2007-01-12 00:14:55 -05:00
end
2005-07-09 13:14:14 -04:00
2009-05-18 02:35:47 -04:00
def test_limited_eager_with_numeric_in_association
2012-01-16 16:14:34 -05:00
assert_equal (
people ( :david , :susan ) ,
2012-07-27 12:27:47 -04:00
Person . references ( :number1_fans_people ) . merge (
2012-04-27 08:11:13 -04:00
:includes = > [ :readers , :primary_contact , :number1_fan ] ,
:where = > " number1_fans_people.first_name like 'M%' " ,
2012-01-16 16:14:34 -05:00
:order = > 'people.id' , :limit = > 2 , :offset = > 0
2012-07-27 07:01:25 -04:00
) . to_a
2012-01-16 16:14:34 -05:00
)
2009-05-18 02:35:47 -04:00
end
2008-01-18 23:19:53 -05:00
def test_preload_with_interpolation
2011-02-11 17:22:19 -05:00
post = Post . includes ( :comments_with_interpolated_conditions ) . find ( posts ( :welcome ) . id )
assert_equal [ comments ( :greetings ) ] , post . comments_with_interpolated_conditions
post = Post . joins ( :comments_with_interpolated_conditions ) . find ( posts ( :welcome ) . id )
assert_equal [ comments ( :greetings ) ] , post . comments_with_interpolated_conditions
2008-01-18 23:19:53 -05:00
end
def test_polymorphic_type_condition
2012-07-27 12:27:47 -04:00
post = Post . all . merge! ( :includes = > :taggings ) . find ( posts ( :thinking ) . id )
2008-01-18 23:19:53 -05:00
assert post . taggings . include? ( taggings ( :thinking_general ) )
2012-07-27 12:27:47 -04:00
post = SpecialPost . all . merge! ( :includes = > :taggings ) . find ( posts ( :thinking ) . id )
2008-01-18 23:19:53 -05:00
assert post . taggings . include? ( taggings ( :thinking_general ) )
end
2006-03-16 01:17:54 -05:00
def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
# Eager includes of has many and habtm associations aren't necessarily sorted in the same way
def assert_equal_after_sort ( item1 , item2 , item3 = nil )
assert_equal ( item1 . sort { | a , b | a . id < = > b . id } , item2 . sort { | a , b | a . id < = > b . id } )
assert_equal ( item3 . sort { | a , b | a . id < = > b . id } , item2 . sort { | a , b | a . id < = > b . id } ) if item3
end
# Test regular association, association with conditions, association with
# STI, and association with conditions assured not to be true
2006-10-08 22:46:57 -04:00
post_types = [ :posts , :other_posts , :special_posts ]
2006-03-16 01:17:54 -05:00
# test both has_many and has_and_belongs_to_many
[ Author , Category ] . each do | className |
d1 = find_all_ordered ( className )
# test including all post types at once
2008-01-18 02:30:42 -05:00
d2 = find_all_ordered ( className , post_types )
d1 . each_index do | i |
2006-03-16 01:17:54 -05:00
assert_equal ( d1 [ i ] , d2 [ i ] )
assert_equal_after_sort ( d1 [ i ] . posts , d2 [ i ] . posts )
post_types [ 1 .. - 1 ] . each do | post_type |
# test including post_types together
d3 = find_all_ordered ( className , [ :posts , post_type ] )
assert_equal ( d1 [ i ] , d3 [ i ] )
assert_equal_after_sort ( d1 [ i ] . posts , d3 [ i ] . posts )
assert_equal_after_sort ( d1 [ i ] . send ( post_type ) , d2 [ i ] . send ( post_type ) , d3 [ i ] . send ( post_type ) )
end
end
end
end
2008-01-18 02:30:42 -05:00
2006-03-16 01:17:54 -05:00
def test_eager_with_multiple_associations_with_same_table_has_one
d1 = find_all_ordered ( Firm )
d2 = find_all_ordered ( Firm , :account )
2008-01-18 02:30:42 -05:00
d1 . each_index do | i |
2006-03-16 01:17:54 -05:00
assert_equal ( d1 [ i ] , d2 [ i ] )
assert_equal ( d1 [ i ] . account , d2 [ i ] . account )
end
end
2008-01-18 02:30:42 -05:00
2006-03-16 01:17:54 -05:00
def test_eager_with_multiple_associations_with_same_table_belongs_to
firm_types = [ :firm , :firm_with_basic_id , :firm_with_other_name , :firm_with_condition ]
d1 = find_all_ordered ( Client )
d2 = find_all_ordered ( Client , firm_types )
2008-01-18 02:30:42 -05:00
d1 . each_index do | i |
2006-03-16 01:17:54 -05:00
assert_equal ( d1 [ i ] , d2 [ i ] )
firm_types . each { | type | assert_equal ( d1 [ i ] . send ( type ) , d2 [ i ] . send ( type ) ) }
end
end
2005-09-18 16:41:44 -04:00
def test_eager_with_valid_association_as_string_not_symbol
2012-07-27 12:27:47 -04:00
assert_nothing_raised { Post . all . merge! ( :includes = > 'comments' ) . to_a }
2005-09-18 16:41:44 -04:00
end
2008-07-02 21:27:42 -04:00
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
2012-07-27 12:27:47 -04:00
Comment . all . merge! ( :where = > " 123.456 = 123.456 " , :includes = > :post ) . to_a
2008-07-02 21:27:42 -04:00
end
end
2005-11-06 15:39:34 -05:00
def test_preconfigured_includes_with_belongs_to
author = posts ( :welcome ) . author_with_posts
2008-01-18 23:19:53 -05:00
assert_no_queries { assert_equal 5 , author . posts . size }
2005-11-06 15:39:34 -05:00
end
def test_preconfigured_includes_with_has_one
comment = posts ( :sti_comments ) . very_special_comment_with_post
2008-01-18 23:19:53 -05:00
assert_no_queries { assert_equal posts ( :sti_comments ) , comment . post }
2005-11-06 15:39:34 -05:00
end
def test_preconfigured_includes_with_has_many
posts = authors ( :david ) . posts_with_comments
2005-12-08 18:23:34 -05:00
one = posts . detect { | p | p . id == 1 }
2008-01-18 23:19:53 -05:00
assert_no_queries do
assert_equal 5 , posts . size
assert_equal 2 , one . comments . size
end
2005-11-06 15:39:34 -05:00
end
def test_preconfigured_includes_with_habtm
posts = authors ( :david ) . posts_with_categories
2005-12-08 18:23:34 -05:00
one = posts . detect { | p | p . id == 1 }
2008-01-18 23:19:53 -05:00
assert_no_queries do
assert_equal 5 , posts . size
assert_equal 2 , one . categories . size
end
2005-11-06 15:39:34 -05:00
end
def test_preconfigured_includes_with_has_many_and_habtm
posts = authors ( :david ) . posts_with_comments_and_categories
2005-12-08 18:23:34 -05:00
one = posts . detect { | p | p . id == 1 }
2008-01-18 23:19:53 -05:00
assert_no_queries do
assert_equal 5 , posts . size
assert_equal 2 , one . comments . size
assert_equal 2 , one . categories . size
end
2005-11-06 15:39:34 -05:00
end
2008-01-18 02:30:42 -05:00
2006-07-24 00:55:16 -04:00
def test_count_with_include
2008-11-19 11:09:44 -05:00
if current_adapter? ( :SybaseAdapter )
2012-04-13 13:26:04 -04:00
assert_equal 3 , authors ( :david ) . posts_with_comments . where ( " len(comments.body) > 15 " ) . references ( :comments ) . count
2007-09-13 19:21:14 -04:00
elsif current_adapter? ( :OpenBaseAdapter )
2012-04-13 13:26:04 -04:00
assert_equal 3 , authors ( :david ) . posts_with_comments . where ( " length(FETCHBLOB(comments.body)) > 15 " ) . references ( :comments ) . count
2006-08-24 23:30:21 -04:00
else
2012-04-13 13:26:04 -04:00
assert_equal 3 , authors ( :david ) . posts_with_comments . where ( " length(comments.body) > 15 " ) . references ( :comments ) . count
2006-08-24 23:30:21 -04:00
end
2006-07-24 00:55:16 -04:00
end
2008-05-06 19:12:17 -04:00
def test_load_with_sti_sharing_association
assert_queries ( 2 ) do #should not do 1 query per subclass
2012-07-27 07:01:25 -04:00
Comment . includes ( :post ) . to_a
2008-05-06 19:12:17 -04:00
end
end
2008-06-08 12:00:56 -04:00
def test_conditions_on_join_table_with_include_and_limit
2012-07-27 12:27:47 -04:00
assert_equal 3 , Developer . all . merge! ( :includes = > 'projects' , :where = > { 'developers_projects.access_level' = > 1 } , :limit = > 5 ) . to_a . size
2008-06-08 12:00:56 -04:00
end
def test_order_on_join_table_with_include_and_limit
2012-07-27 12:27:47 -04:00
assert_equal 5 , Developer . all . merge! ( :includes = > 'projects' , :order = > 'developers_projects.joined_on DESC' , :limit = > 5 ) . to_a . size
2008-06-08 12:00:56 -04:00
end
2008-12-17 18:39:09 -05:00
2008-12-18 14:07:55 -05:00
def test_eager_loading_with_order_on_joined_table_preloads
posts = assert_queries ( 2 ) do
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :joins = > :comments , :includes = > :author , :order = > 'comments.id DESC' ) . to_a
2008-12-18 14:07:55 -05:00
end
2010-10-06 07:06:51 -04:00
assert_equal posts ( :eager_other ) , posts [ 1 ]
assert_equal authors ( :mary ) , assert_no_queries { posts [ 1 ] . author }
2008-12-18 14:07:55 -05:00
end
def test_eager_loading_with_conditions_on_joined_table_preloads
posts = assert_queries ( 2 ) do
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :select = > 'distinct posts.*' , :includes = > :author , :joins = > [ :comments ] , :where = > " comments.body like 'Thank you%' " , :order = > 'posts.id' ) . to_a
2008-12-18 14:07:55 -05:00
end
assert_equal [ posts ( :welcome ) ] , posts
assert_equal authors ( :david ) , assert_no_queries { posts [ 0 ] . author }
2012-03-01 22:10:06 -05:00
posts = assert_queries ( 2 ) do
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :select = > 'distinct posts.*' , :includes = > :author , :joins = > [ :comments ] , :where = > " comments.body like 'Thank you%' " , :order = > 'posts.id' ) . to_a
2008-12-18 14:07:55 -05:00
end
assert_equal [ posts ( :welcome ) ] , posts
assert_equal authors ( :david ) , assert_no_queries { posts [ 0 ] . author }
2012-03-01 22:10:06 -05:00
posts = assert_queries ( 2 ) do
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :includes = > :author , :joins = > { :taggings = > :tag } , :where = > " tags.name = 'General' " , :order = > 'posts.id' ) . to_a
2008-12-18 14:07:55 -05:00
end
assert_equal posts ( :welcome , :thinking ) , posts
2012-03-01 22:10:06 -05:00
posts = assert_queries ( 2 ) do
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :includes = > :author , :joins = > { :taggings = > { :tag = > :taggings } } , :where = > " taggings_tags.super_tag_id=2 " , :order = > 'posts.id' ) . to_a
2008-12-18 14:07:55 -05:00
end
assert_equal posts ( :welcome , :thinking ) , posts
end
2012-08-28 15:48:59 -04:00
def test_preload_has_many_with_association_condition_and_default_scope
post = Post . create! ( :title = > 'Beaches' , :body = > " I like beaches! " )
Reader . create! :person = > people ( :david ) , :post = > post
LazyReader . create! :person = > people ( :susan ) , :post = > post
assert_equal 1 , post . lazy_readers . to_a . size
assert_equal 2 , post . lazy_readers_skimmers_or_not . to_a . size
post_with_readers = Post . includes ( :lazy_readers_skimmers_or_not ) . find ( post . id )
assert_equal 2 , post_with_readers . lazy_readers_skimmers_or_not . to_a . size
end
2008-12-18 14:07:55 -05:00
def test_eager_loading_with_conditions_on_string_joined_table_preloads
posts = assert_queries ( 2 ) do
2012-07-27 12:27:47 -04:00
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
2008-12-18 14:07:55 -05:00
end
assert_equal [ posts ( :welcome ) ] , posts
assert_equal authors ( :david ) , assert_no_queries { posts [ 0 ] . author }
2012-03-01 22:10:06 -05:00
posts = assert_queries ( 2 ) do
2012-07-27 12:27:47 -04:00
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
2008-12-18 14:07:55 -05:00
end
assert_equal [ posts ( :welcome ) ] , posts
assert_equal authors ( :david ) , assert_no_queries { posts [ 0 ] . author }
end
def test_eager_loading_with_select_on_joined_table_preloads
posts = assert_queries ( 2 ) do
2012-07-27 12:27:47 -04:00
Post . all . merge! ( :select = > 'posts.*, authors.name as author_name' , :includes = > :comments , :joins = > :author , :order = > 'posts.id' ) . to_a
2008-12-18 14:07:55 -05:00
end
assert_equal 'David' , posts [ 0 ] . author_name
assert_equal posts ( :welcome ) . comments , assert_no_queries { posts [ 0 ] . comments }
end
def test_eager_loading_with_conditions_on_join_model_preloads
authors = assert_queries ( 2 ) do
2012-07-27 12:27:47 -04:00
Author . all . merge! ( :includes = > :author_address , :joins = > :comments , :where = > " posts.title like 'Welcome%' " ) . to_a
2008-12-18 14:07:55 -05:00
end
assert_equal authors ( :david ) , authors [ 0 ]
assert_equal author_addresses ( :david_address ) , authors [ 0 ] . author_address
end
2008-12-18 20:02:21 -05:00
def test_preload_belongs_to_uses_exclusive_scope
2012-07-27 12:27:47 -04:00
people = Person . males . merge ( :includes = > :primary_contact ) . to_a
2008-12-18 20:02:21 -05:00
assert_not_equal people . length , 0
people . each do | person |
assert_no_queries { assert_not_nil person . primary_contact }
assert_equal Person . find ( person . id ) . primary_contact , person . primary_contact
end
end
def test_preload_has_many_uses_exclusive_scope
2012-07-27 07:01:25 -04:00
people = Person . males . includes ( :agents ) . to_a
2008-12-18 20:02:21 -05:00
people . each do | person |
assert_equal Person . find ( person . id ) . agents , person . agents
end
end
2008-12-26 17:53:07 -05:00
def test_preload_has_many_using_primary_key
2012-04-26 13:32:55 -04:00
expected = Firm . first . clients_using_primary_key . to_a
2012-04-27 06:42:22 -04:00
firm = Firm . includes ( :clients_using_primary_key ) . first
2008-12-26 17:53:07 -05:00
assert_no_queries do
assert_equal expected , firm . clients_using_primary_key
end
end
def test_include_has_many_using_primary_key
2010-03-15 21:09:33 -04:00
expected = Firm . find ( 1 ) . clients_using_primary_key . sort_by ( & :name )
2009-03-22 17:57:24 -04:00
# Oracle adapter truncates alias to 30 characters
if current_adapter? ( :OracleAdapter )
2012-07-27 12:27:47 -04:00
firm = Firm . all . merge! ( :includes = > :clients_using_primary_key , :order = > 'clients_using_primary_keys_companies' [ 0 , 30 ] + '.name' ) . find ( 1 )
2009-03-22 17:57:24 -04:00
else
2012-07-27 12:27:47 -04:00
firm = Firm . all . merge! ( :includes = > :clients_using_primary_key , :order = > 'clients_using_primary_keys_companies.name' ) . find ( 1 )
2009-03-22 17:57:24 -04:00
end
2008-12-26 17:53:07 -05:00
assert_no_queries do
assert_equal expected , firm . clients_using_primary_key
end
end
2009-08-18 06:50:11 -04:00
2008-12-26 18:26:37 -05:00
def test_preload_has_one_using_primary_key
2010-12-23 19:14:46 -05:00
expected = accounts ( :signals37 )
2012-07-27 12:27:47 -04:00
firm = Firm . all . merge! ( :includes = > :account_using_primary_key , :order = > 'companies.id' ) . first
2008-12-26 18:26:37 -05:00
assert_no_queries do
assert_equal expected , firm . account_using_primary_key
end
end
def test_include_has_one_using_primary_key
2010-12-23 19:14:46 -05:00
expected = accounts ( :signals37 )
2012-07-27 12:27:47 -04:00
firm = Firm . all . merge! ( :includes = > :account_using_primary_key , :order = > 'accounts.id' ) . to_a . detect { | f | f . id == 1 }
2008-12-26 18:26:37 -05:00
assert_no_queries do
assert_equal expected , firm . account_using_primary_key
end
end
2009-08-18 06:50:11 -04:00
2011-01-02 09:28:53 -05:00
def test_preloading_empty_belongs_to
c = Client . create! ( :name = > 'Foo' , :client_of = > Company . maximum ( :id ) + 1 )
client = assert_queries ( 2 ) { Client . preload ( :firm ) . find ( c . id ) }
assert_no_queries { assert_nil client . firm }
end
def test_preloading_empty_belongs_to_polymorphic
2010-03-31 07:55:49 -04:00
t = Tagging . create! ( :taggable_type = > 'Post' , :taggable_id = > Post . maximum ( :id ) + 1 , :tag = > tags ( :general ) )
2012-03-01 22:10:06 -05:00
tagging = assert_queries ( 2 ) { Tagging . preload ( :taggable ) . find ( t . id ) }
2011-01-02 09:28:53 -05:00
assert_no_queries { assert_nil tagging . taggable }
end
def test_preloading_through_empty_belongs_to
c = Client . create! ( :name = > 'Foo' , :client_of = > Company . maximum ( :id ) + 1 )
client = assert_queries ( 2 ) { Client . preload ( :accounts ) . find ( c . id ) }
assert_no_queries { assert client . accounts . empty? }
2010-03-31 07:55:49 -04:00
end
2010-12-15 19:40:03 -05:00
def test_preloading_has_many_through_with_uniq
mary = Author . includes ( :unique_categorized_posts ) . where ( :id = > authors ( :mary ) . id ) . first
assert_equal 1 , mary . unique_categorized_posts . length
assert_equal 1 , mary . unique_categorized_post_ids . length
end
2011-01-01 13:18:54 -05:00
def test_preloading_polymorphic_with_custom_foreign_type
sponsor = sponsors ( :moustache_club_sponsor_for_groucho )
groucho = members ( :groucho )
sponsor = assert_queries ( 2 ) {
Sponsor . includes ( :thing ) . where ( :id = > sponsor . id ) . first
}
assert_no_queries { assert_equal groucho , sponsor . thing }
2010-03-31 07:55:49 -04:00
end
2011-07-09 06:37:48 -04:00
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 }
assert_queries ( 0 ) do
assert_not_equal 0 , post . comments . to_a . count
end
end
2011-07-18 16:56:07 -04:00
def test_join_eager_with_empty_order_should_generate_valid_sql
assert_nothing_raised ( ActiveRecord :: StatementInvalid ) do
2012-01-13 18:42:07 -05:00
Post . includes ( :comments ) . order ( " " ) . where ( :comments = > { :body = > " Thank you for the welcome " } ) . first
2011-07-18 16:56:07 -04:00
end
end
def test_join_eager_with_nil_order_should_generate_valid_sql
assert_nothing_raised ( ActiveRecord :: StatementInvalid ) do
2012-01-13 18:42:07 -05:00
Post . includes ( :comments ) . order ( nil ) . where ( :comments = > { :body = > " Thank you for the welcome " } ) . first
2011-07-18 16:56:07 -04:00
end
end
2011-12-21 05:46:55 -05:00
def test_deep_including_through_habtm
2012-07-27 12:27:47 -04:00
posts = Post . all . merge! ( :includes = > { :categories = > :categorizations } , :order = > " posts.id " ) . to_a
2011-12-21 05:46:55 -05:00
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 }
end
2012-03-30 11:18:39 -04:00
test " scoping with a circular preload " do
assert_equal Comment . find ( 1 ) , Comment . preload ( :post = > :comments ) . scoping { Comment . find ( 1 ) }
end
2012-04-03 15:13:24 -04:00
test " circular preload does not modify unscoped " do
expected = FirstPost . unscoped . find ( 2 )
FirstPost . preload ( :comments = > :first_post ) . find ( 1 )
assert_equal expected , FirstPost . unscoped . find ( 2 )
end
2012-03-30 11:18:39 -04:00
test " preload ignores the scoping " do
assert_equal (
Comment . find ( 1 ) . post ,
Post . where ( '1 = 0' ) . scoping { Comment . preload ( :post ) . find ( 1 ) . post }
)
end
2005-04-03 13:50:11 -04:00
end