3 require 'models/tagging'
5 require 'models/comment'
6 require 'models/author'
7 require 'models/category'
8 require 'models/company'
9 require 'models/person'
10 require 'models/reader'
11 require 'models/owner'
13 require 'models/reference'
15 require 'models/subscriber'
16 require 'models/subscription'
18 require 'models/developer'
19 require 'models/project'
21 class EagerAssociationTest
< ActiveRecord
::TestCase
22 fixtures
:posts, :comments, :authors, :author_addresses, :categories, :categories_posts,
23 :companies, :accounts, :tags, :taggings, :people, :readers,
24 :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
25 :developers, :projects, :developers_projects
27 def test_loading_with_one_association
28 posts
= Post
.find(:all, :include => :comments)
29 post
= posts
.find
{ |p
| p
.id
== 1 }
30 assert_equal
2, post
.comments
.size
31 assert post
.comments
.include?(comments(:greetings))
33 post
= Post
.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
34 assert_equal
2, post
.comments
.size
35 assert post
.comments
.include?(comments(:greetings))
37 posts
= Post
.find(:all, :include => :last_comment)
38 post
= posts
.find
{ |p
| p
.id
== 1 }
39 assert_equal Post
.find(1).last_comment
, post
.last_comment
42 def test_loading_with_one_association_with_non_preload
43 posts
= Post
.find(:all, :include => :last_comment, :order => 'comments.id DESC')
44 post
= posts
.find
{ |p
| p
.id
== 1 }
45 assert_equal Post
.find(1).last_comment
, post
.last_comment
48 def test_loading_conditions_with_or
49 posts
= authors(:david).posts
.find(:all, :include => :comments, :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'")
50 assert_nil posts
.detect
{ |p
| p
.author_id
!= authors(:david).id
},
51 "expected to find only david's posts"
54 def test_with_ordering
55 list
= Post
.find(:all, :include => :comments, :order => "posts.id DESC")
56 [:eager_other, :sti_habtm, :sti_post_and_comments, :sti_comments,
57 :authorless, :thinking, :welcome
58 ].each_with_index
do |post
, index
|
59 assert_equal
posts(post
), list
[index
]
63 def test_with_two_tables_in_from_without_getting_double_quoted
64 posts
= Post
.find(:all,
66 :from => "authors, posts",
67 :include => :comments,
68 :conditions => "posts.author_id = authors.id",
72 assert_equal
2, posts
.first
.comments
.size
75 def test_loading_with_multiple_associations
76 posts
= Post
.find(:all, :include => [ :comments, :author, :categories ], :order => "posts.id")
77 assert_equal
2, posts
.first
.comments
.size
78 assert_equal
2, posts
.first
.categories
.size
79 assert posts
.first
.comments
.include?(comments(:greetings))
82 def test_duplicate_middle_objects
83 comments
= Comment
.find
:all, :conditions => 'post_id = 1', :include => [:post => :author]
85 comments
.each
{|comment
| comment
.post
.author
.name
}
89 def test_including_duplicate_objects_from_belongs_to
90 popular_post
= Post
.create
!(:title => 'foo', :body => "I like cars!")
91 comment
= popular_post
.comments
.create
!(:body => "lol")
92 popular_post
.readers
.create
!(:person => people(:michael))
93 popular_post
.readers
.create
!(:person => people(:david))
95 readers
= Reader
.find(:all, :conditions => ["post_id = ?", popular_post
.id
],
96 :include => {:post => :comments})
97 readers
.each
do |reader
|
98 assert_equal
[comment
], reader
.post
.comments
102 def test_including_duplicate_objects_from_has_many
103 car_post
= Post
.create
!(:title => 'foo', :body => "I like cars!")
104 car_post
.categories
<< categories(:general)
105 car_post
.categories
<< categories(:technology)
107 comment
= car_post
.comments
.create
!(:body => "hmm")
108 categories
= Category
.find(:all, :conditions => ["posts.id=?", car_post
.id
],
109 :include => {:posts => :comments})
110 categories
.each
do |category
|
111 assert_equal
[comment
], category
.posts
[0].comments
115 def test_finding_with_includes_on_has_many_association_with_same_include_includes_only_once
116 author_id
= authors(:david).id
117 author
= assert_queries(3) { Author
.find(author_id
, :include => {:posts_with_comments => :comments}) } # find the author, then find the posts, then find the comments
118 author
.posts_with_comments
.each
do |post_with_comments
|
119 assert_equal post_with_comments
.comments
.length
, post_with_comments
.comments
.count
120 assert_equal
nil, post_with_comments
.comments
.uniq
!
124 def test_finding_with_includes_on_has_one_assocation_with_same_include_includes_only_once
125 author
= authors(:david)
126 post
= author
.post_about_thinking_with_last_comment
127 last_comment
= post
.last_comment
128 author
= assert_queries(3) { Author
.find(author
.id
, :include => {:post_about_thinking_with_last_comment => :last_comment})} # find the author, then find the posts, then find the comments
130 assert_equal post
, author
.post_about_thinking_with_last_comment
131 assert_equal last_comment
, author
.post_about_thinking_with_last_comment
.last_comment
135 def test_finding_with_includes_on_belongs_to_association_with_same_include_includes_only_once
136 post
= posts(:welcome)
138 author_address
= author
.author_address
139 post
= assert_queries(3) { Post
.find(post
.id
, :include => {:author_with_address => :author_address}) } # find the post, then find the author, then find the address
141 assert_equal author
, post
.author_with_address
142 assert_equal author_address
, post
.author_with_address
.author_address
146 def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once
147 post
= posts(:welcome)
148 post
.update_attributes
!(:author => nil)
149 post
= assert_queries(1) { Post
.find(post
.id
, :include => {:author_with_address => :author_address}) } # find the post, then find the author which is null so no query for the author or address
151 assert_equal
nil, post
.author_with_address
155 def test_loading_from_an_association
156 posts
= authors(:david).posts
.find(:all, :include => :comments, :order => "posts.id")
157 assert_equal
2, posts
.first
.comments
.size
160 def test_loading_from_an_association_that_has_a_hash_of_conditions
161 assert_nothing_raised
do
162 Author
.find(:all, :include => :hello_posts_with_hash_conditions)
164 assert
!Author
.find(authors(:david).id
, :include => :hello_posts_with_hash_conditions).hello_posts
.empty
?
167 def test_loading_with_no_associations
168 assert_nil Post
.find(posts(:authorless).id
, :include => :author).author
171 def test_nested_loading_with_no_associations
172 assert_nothing_raised
do
173 Post
.find(posts(:authorless).id
, :include => {:author => :author_addresss})
177 def test_eager_association_loading_with_belongs_to_and_foreign_keys
178 pets
= Pet
.find(:all, :include => :owner)
179 assert_equal
3, pets
.length
182 def test_eager_association_loading_with_belongs_to
183 comments
= Comment
.find(:all, :include => :post)
184 assert_equal
10, comments
.length
185 titles
= comments
.map
{ |c
| c
.post
.title
}
186 assert titles
.include?(posts(:welcome).title
)
187 assert titles
.include?(posts(:sti_post_and_comments).title
)
190 def test_eager_association_loading_with_belongs_to_and_limit
191 comments
= Comment
.find(:all, :include => :post, :limit => 5, :order => 'comments.id')
192 assert_equal
5, comments
.length
193 assert_equal
[1,2,3,5,6], comments
.collect
{ |c
| c
.id
}
196 def test_eager_association_loading_with_belongs_to_and_limit_and_conditions
197 comments
= Comment
.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :order => 'comments.id')
198 assert_equal
3, comments
.length
199 assert_equal
[5,6,7], comments
.collect
{ |c
| c
.id
}
202 def test_eager_association_loading_with_belongs_to_and_limit_and_offset
203 comments
= Comment
.find(:all, :include => :post, :limit => 3, :offset => 2, :order => 'comments.id')
204 assert_equal
3, comments
.length
205 assert_equal
[3,5,6], comments
.collect
{ |c
| c
.id
}
208 def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions
209 comments
= Comment
.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :offset => 1, :order => 'comments.id')
210 assert_equal
3, comments
.length
211 assert_equal
[6,7,8], comments
.collect
{ |c
| c
.id
}
214 def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
215 comments
= Comment
.find(:all, :include => :post, :conditions => ['post_id = ?',4], :limit => 3, :offset => 1, :order => 'comments.id')
216 assert_equal
3, comments
.length
217 assert_equal
[6,7,8], comments
.collect
{ |c
| c
.id
}
220 def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name
221 assert_nothing_raised
do
222 Comment
.find(:all, :include => :post, :conditions => ['posts.id = ?',4])
226 def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
227 quoted_posts_id
= Comment
.connection
.quote_table_name('posts') + '.' + Comment
.connection
.quote_column_name('id')
228 assert_nothing_raised
do
229 Comment
.find(:all, :include => :post, :conditions => ["#{quoted_posts_id} = ?",4])
233 def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
234 assert_nothing_raised
do
235 Comment
.find(:all, :include => :post, :order => 'posts.id')
239 def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
240 quoted_posts_id
= Comment
.connection
.quote_table_name('posts') + '.' + Comment
.connection
.quote_column_name('id')
241 assert_nothing_raised
do
242 Comment
.find(:all, :include => :post, :order => quoted_posts_id
)
246 def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
247 posts
= Post
.find(:all, :include => [:author, :very_special_comment], :limit => 1, :order => 'posts.id')
248 assert_equal
1, posts
.length
249 assert_equal
[1], posts
.collect
{ |p
| p
.id
}
252 def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
253 posts
= Post
.find(:all, :include => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id')
254 assert_equal
1, posts
.length
255 assert_equal
[2], posts
.collect
{ |p
| p
.id
}
258 def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name
259 author_favorite
= AuthorFavorite
.find(:first, :include => :favorite_author)
260 assert_equal
authors(:mary), assert_no_queries
{ author_favorite
.favorite_author
}
263 def test_eager_load_belongs_to_quotes_table_and_column_names
264 job
= Job
.find
jobs(:unicyclist).id
, :include => :ideal_reference
265 references(:michael_unicyclist)
266 assert_no_queries
{ assert_equal
references(:michael_unicyclist), job
.ideal_reference
}
269 def test_eager_load_has_one_quotes_table_and_column_names
270 michael
= Person
.find(people(:michael), :include => :favourite_reference)
271 references(:michael_unicyclist)
272 assert_no_queries
{ assert_equal
references(:michael_unicyclist), michael
.favourite_reference
}
275 def test_eager_load_has_many_quotes_table_and_column_names
276 michael
= Person
.find(people(:michael), :include => :references)
277 references(:michael_magician,:michael_unicyclist)
278 assert_no_queries
{ assert_equal
references(:michael_magician,:michael_unicyclist), michael
.references
.sort_by(&:id) }
281 def test_eager_load_has_many_through_quotes_table_and_column_names
282 michael
= Person
.find(people(:michael), :include => :jobs)
283 jobs(:magician, :unicyclist)
284 assert_no_queries
{ assert_equal
jobs(:unicyclist, :magician), michael
.jobs
.sort_by(&:id) }
287 def test_eager_load_has_many_with_string_keys
288 subscriptions
= subscriptions(:webster_awdr, :webster_rfr)
289 subscriber
=Subscriber
.find(subscribers(:second).id
, :include => :subscriptions)
290 assert_equal subscriptions
, subscriber
.subscriptions
.sort_by(&:id)
293 def test_eager_load_has_many_through_with_string_keys
294 books
= books(:awdr, :rfr)
295 subscriber
= Subscriber
.find(subscribers(:second).id
, :include => :books)
296 assert_equal books
, subscriber
.books
.sort_by(&:id)
299 def test_eager_load_belongs_to_with_string_keys
300 subscriber
= subscribers(:second)
301 subscription
= Subscription
.find(subscriptions(:webster_awdr).id
, :include => :subscriber)
302 assert_equal subscriber
, subscription
.subscriber
305 def test_eager_association_loading_with_explicit_join
306 posts
= Post
.find(:all, :include => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id')
307 assert_equal
1, posts
.length
310 def test_eager_with_has_many_through
311 posts_with_comments
= people(:michael).posts
.find(:all, :include => :comments, :order => 'posts.id')
312 posts_with_author
= people(:michael).posts
.find(:all, :include => :author, :order => 'posts.id')
313 posts_with_comments_and_author
= people(:michael).posts
.find(:all, :include => [ :comments, :author ], :order => 'posts.id')
314 assert_equal
2, posts_with_comments
.inject(0) { |sum
, post
| sum
+= post
.comments
.size
}
315 assert_equal
authors(:david), assert_no_queries
{ posts_with_author
.first
.author
}
316 assert_equal
authors(:david), assert_no_queries
{ posts_with_comments_and_author
.first
.author
}
319 def test_eager_with_has_many_through_a_belongs_to_association
320 author
= authors(:mary)
321 post
= Post
.create
!(:author => author
, :title => "TITLE", :body => "BODY")
322 author
.author_favorites
.create(:favorite_author_id => 1)
323 author
.author_favorites
.create(:favorite_author_id => 2)
324 posts_with_author_favorites
= author
.posts
.find(:all, :include => :author_favorites)
325 assert_no_queries
{ posts_with_author_favorites
.first
.author_favorites
.first
.author_id
}
328 def test_eager_with_has_many_through_an_sti_join_model
329 author
= Author
.find(:first, :include => :special_post_comments, :order => 'authors.id')
330 assert_equal
[comments(:does_it_hurt)], assert_no_queries
{ author
.special_post_comments
}
333 def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
334 author
= Author
.find(:first, :include => :special_nonexistant_post_comments, :order => 'authors.id')
335 assert_equal
[], author
.special_nonexistant_post_comments
338 def test_eager_with_has_many_through_join_model_with_conditions
339 assert_equal Author
.find(:first, :include => :hello_post_comments,
340 :order => 'authors.id').hello_post_comments
.sort_by(&:id),
341 Author
.find(:first, :order => 'authors.id').hello_post_comments
.sort_by(&:id)
344 def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
345 assert_equal
comments(:more_greetings), Author
.find(authors(:david).id
, :include => :comments_with_order_and_conditions).comments_with_order_and_conditions
.first
348 def test_eager_with_has_many_through_join_model_with_include
349 author_comments
= Author
.find(authors(:david).id
, :include => :comments_with_include).comments_with_include
.to_a
351 author_comments
.first
.post
.title
355 def test_eager_with_has_many_and_limit
356 posts
= Post
.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
357 assert_equal
2, posts
.size
358 assert_equal
3, posts
.inject(0) { |sum
, post
| sum
+= post
.comments
.size
}
361 def test_eager_with_has_many_and_limit_and_conditions
362 if current_adapter
?(:OpenBaseAdapter)
363 posts
= Post
.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "FETCHBLOB(posts.body) = 'hello'", :order => "posts.id")
365 posts
= Post
.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.body = 'hello'", :order => "posts.id")
367 assert_equal
2, posts
.size
368 assert_equal
[4,5], posts
.collect
{ |p
| p
.id
}
371 def test_eager_with_has_many_and_limit_and_conditions_array
372 if current_adapter
?(:OpenBaseAdapter)
373 posts
= Post
.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "FETCHBLOB(posts.body) = ?", 'hello' ], :order => "posts.id")
375 posts
= Post
.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "posts.body = ?", 'hello' ], :order => "posts.id")
377 assert_equal
2, posts
.size
378 assert_equal
[4,5], posts
.collect
{ |p
| p
.id
}
381 def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
382 posts
= Post
.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
383 assert_equal
2, posts
.size
385 count
= Post
.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
386 assert_equal count
, posts
.size
389 def test_eager_with_has_many_and_limit_and_high_offset
390 posts
= Post
.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
391 assert_equal
0, posts
.size
394 def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions
396 posts
= Post
.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
397 :conditions => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ])
398 assert_equal
0, posts
.size
402 def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash_conditions
404 posts
= Post
.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
405 :conditions => { 'authors.name' => 'David', 'comments.body' => 'go crazy' })
406 assert_equal
0, posts
.size
410 def test_count_eager_with_has_many_and_limit_and_high_offset
411 posts
= Post
.count(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
412 assert_equal
0, posts
415 def test_eager_with_has_many_and_limit_with_no_results
416 posts
= Post
.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.title = 'magic forest'")
417 assert_equal
0, posts
.size
420 def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
421 author
= authors(:david)
422 author_posts_without_comments
= author
.posts
.select
{ |post
| post
.comments
.blank
? }
423 assert_equal author_posts_without_comments
.size
, author
.posts
.count(:all, :include => :comments, :conditions => 'comments.id is null')
426 def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
427 person
= people(:michael)
428 person_posts_without_comments
= person
.posts
.select
{ |post
| post
.comments
.blank
? }
429 assert_equal person_posts_without_comments
.size
, person
.posts_with_no_comments
.count
432 def test_eager_with_has_and_belongs_to_many_and_limit
433 posts
= Post
.find(:all, :include => :categories, :order => "posts.id", :limit => 3)
434 assert_equal
3, posts
.size
435 assert_equal
2, posts
[0].categories
.size
436 assert_equal
1, posts
[1].categories
.size
437 assert_equal
0, posts
[2].categories
.size
438 assert posts
[0].categories
.include?(categories(:technology))
439 assert posts
[1].categories
.include?(categories(:general))
442 def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
443 posts
= authors(:david).posts
.find(:all,
444 :include => :comments,
445 :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
448 assert_equal
2, posts
.size
451 :include => [ :comments, :author ],
452 :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
455 assert_equal count
, posts
.size
458 def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
460 Post
.with_scope(:find => {
461 :include => :comments,
462 :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
464 posts
= authors(:david).posts
.find(:all, :limit => 2)
465 assert_equal
2, posts
.size
468 Post
.with_scope(:find => {
469 :include => [ :comments, :author ],
470 :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
472 count
= Post
.count(:limit => 2)
473 assert_equal count
, posts
.size
477 def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
478 Post
.with_scope(:find => { :conditions => "1=1" }) do
479 posts
= authors(:david).posts
.find(:all,
480 :include => :comments,
481 :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
484 assert_equal
2, posts
.size
487 :include => [ :comments, :author ],
488 :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
491 assert_equal count
, posts
.size
495 def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
496 posts_with_explicit_order
= Post
.find(:all, :conditions => 'comments.id is not null', :include => :comments, :order => 'posts.id DESC', :limit => 2)
497 posts_with_scoped_order
= Post
.with_scope(:find => {:order => 'posts.id DESC'}) do
498 Post
.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2)
500 assert_equal posts_with_explicit_order
, posts_with_scoped_order
503 def test_eager_association_loading_with_habtm
504 posts
= Post
.find(:all, :include => :categories, :order => "posts.id")
505 assert_equal
2, posts
[0].categories
.size
506 assert_equal
1, posts
[1].categories
.size
507 assert_equal
0, posts
[2].categories
.size
508 assert posts
[0].categories
.include?(categories(:technology))
509 assert posts
[1].categories
.include?(categories(:general))
512 def test_eager_with_inheritance
513 posts
= SpecialPost
.find(:all, :include => [ :comments ])
516 def test_eager_has_one_with_association_inheritance
517 post
= Post
.find(4, :include => [ :very_special_comment ])
518 assert_equal
"VerySpecialComment", post
.very_special_comment
.class.to_s
521 def test_eager_has_many_with_association_inheritance
522 post
= Post
.find(4, :include => [ :special_comments ])
523 post
.special_comments
.each
do |special_comment
|
524 assert_equal
"SpecialComment", special_comment
.class.to_s
528 def test_eager_habtm_with_association_inheritance
529 post
= Post
.find(6, :include => [ :special_categories ])
530 assert_equal
1, post
.special_categories
.size
531 post
.special_categories
.each
do |special_category
|
532 assert_equal
"SpecialCategory", special_category
.class.to_s
536 def test_eager_with_has_one_dependent_does_not_destroy_dependent
537 assert_not_nil
companies(:first_firm).account
538 f
= Firm
.find(:first, :include => :account,
539 :conditions => ["companies.name = ?", "37signals"])
540 assert_not_nil f
.account
541 assert_equal
companies(:first_firm, :reload).account
, f
.account
544 def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
545 author
= authors(:david)
546 posts_with_no_comments
= author
.posts
.select
{ |post
| post
.comments
.blank
? }
547 assert_equal posts_with_no_comments
.size
, author
.posts_with_no_comments
.size
548 assert_equal posts_with_no_comments
, author
.posts_with_no_comments
551 def test_eager_with_invalid_association_reference
552 assert_raise(ActiveRecord
::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
553 post
= Post
.find(6, :include=> :monkeys )
555 assert_raise(ActiveRecord
::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
556 post
= Post
.find(6, :include=>[ :monkeys ])
558 assert_raise(ActiveRecord
::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
559 post
= Post
.find(6, :include=>[ 'monkeys' ])
561 assert_raise(ActiveRecord
::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants") {
562 post
= Post
.find(6, :include=>[ :monkeys, :elephants ])
566 def find_all_ordered(className
, include=nil)
567 className
.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include)
570 def test_limited_eager_with_order
571 assert_equal
posts(:thinking, :sti_comments), Post
.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title)', :limit => 2, :offset => 1)
572 assert_equal
posts(:sti_post_and_comments, :sti_comments), Post
.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title) DESC', :limit => 2, :offset => 1)
575 def test_limited_eager_with_multiple_order_columns
576 assert_equal
posts(:thinking, :sti_comments), Post
.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title), posts.id', :limit => 2, :offset => 1)
577 assert_equal
posts(:sti_post_and_comments, :sti_comments), Post
.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title) DESC, posts.id', :limit => 2, :offset => 1)
580 def test_preload_with_interpolation
581 assert_equal
[comments(:greetings)], Post
.find(posts(:welcome).id
, :include => :comments_with_interpolated_conditions).comments_with_interpolated_conditions
584 def test_polymorphic_type_condition
585 post
= Post
.find(posts(:thinking).id
, :include => :taggings)
586 assert post
.taggings
.include?(taggings(:thinking_general))
587 post
= SpecialPost
.find(posts(:thinking).id
, :include => :taggings)
588 assert post
.taggings
.include?(taggings(:thinking_general))
591 def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
592 # Eager includes of has many and habtm associations aren't necessarily sorted in the same way
593 def assert_equal_after_sort(item1
, item2
, item3
= nil)
594 assert_equal(item1
.sort
{|a
,b
| a
.id
<=> b
.id
}, item2
.sort
{|a
,b
| a
.id
<=> b
.id
})
595 assert_equal(item3
.sort
{|a
,b
| a
.id
<=> b
.id
}, item2
.sort
{|a
,b
| a
.id
<=> b
.id
}) if item3
597 # Test regular association, association with conditions, association with
598 # STI, and association with conditions assured not to be true
599 post_types
= [:posts, :other_posts, :special_posts]
600 # test both has_many and has_and_belongs_to_many
601 [Author
, Category
].each
do |className
|
602 d1
= find_all_ordered(className
)
603 # test including all post types at once
604 d2
= find_all_ordered(className
, post_types
)
606 assert_equal(d1
[i
], d2
[i
])
607 assert_equal_after_sort(d1
[i
].posts
, d2
[i
].posts
)
608 post_types
[1..-1].each
do |post_type
|
609 # test including post_types together
610 d3
= find_all_ordered(className
, [:posts, post_type
])
611 assert_equal(d1
[i
], d3
[i
])
612 assert_equal_after_sort(d1
[i
].posts
, d3
[i
].posts
)
613 assert_equal_after_sort(d1
[i
].send(post_type
), d2
[i
].send(post_type
), d3
[i
].send(post_type
))
619 def test_eager_with_multiple_associations_with_same_table_has_one
620 d1
= find_all_ordered(Firm
)
621 d2
= find_all_ordered(Firm
, :account)
623 assert_equal(d1
[i
], d2
[i
])
624 assert_equal(d1
[i
].account
, d2
[i
].account
)
628 def test_eager_with_multiple_associations_with_same_table_belongs_to
629 firm_types
= [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
630 d1
= find_all_ordered(Client
)
631 d2
= find_all_ordered(Client
, firm_types
)
633 assert_equal(d1
[i
], d2
[i
])
634 firm_types
.each
{ |type
| assert_equal(d1
[i
].send(type
), d2
[i
].send(type
)) }
637 def test_eager_with_valid_association_as_string_not_symbol
638 assert_nothing_raised
{ Post
.find(:all, :include => 'comments') }
641 def test_eager_with_floating_point_numbers
643 # Before changes, the floating point numbers will be interpreted as table names and will cause this to run in one query
644 Comment
.find
:all, :conditions => "123.456 = 123.456", :include => :post
648 def test_preconfigured_includes_with_belongs_to
649 author
= posts(:welcome).author_with_posts
650 assert_no_queries
{assert_equal
5, author
.posts
.size
}
653 def test_preconfigured_includes_with_has_one
654 comment
= posts(:sti_comments).very_special_comment_with_post
655 assert_no_queries
{assert_equal
posts(:sti_comments), comment
.post
}
658 def test_preconfigured_includes_with_has_many
659 posts
= authors(:david).posts_with_comments
660 one
= posts
.detect
{ |p
| p
.id
== 1 }
662 assert_equal
5, posts
.size
663 assert_equal
2, one
.comments
.size
667 def test_preconfigured_includes_with_habtm
668 posts
= authors(:david).posts_with_categories
669 one
= posts
.detect
{ |p
| p
.id
== 1 }
671 assert_equal
5, posts
.size
672 assert_equal
2, one
.categories
.size
676 def test_preconfigured_includes_with_has_many_and_habtm
677 posts
= authors(:david).posts_with_comments_and_categories
678 one
= posts
.detect
{ |p
| p
.id
== 1 }
680 assert_equal
5, posts
.size
681 assert_equal
2, one
.comments
.size
682 assert_equal
2, one
.categories
.size
686 def test_count_with_include
687 if current_adapter
?(:SybaseAdapter)
688 assert_equal
3, authors(:david).posts_with_comments
.count(:conditions => "len(comments.body) > 15")
689 elsif current_adapter
?(:OpenBaseAdapter)
690 assert_equal
3, authors(:david).posts_with_comments
.count(:conditions => "length(FETCHBLOB(comments.body)) > 15")
692 assert_equal
3, authors(:david).posts_with_comments
.count(:conditions => "length(comments.body) > 15")
696 def test_load_with_sti_sharing_association
697 assert_queries(2) do #should not do 1 query per subclass
698 Comment
.find
:all, :include => :post
702 def test_conditions_on_join_table_with_include_and_limit
703 assert_equal
3, Developer
.find(:all, :include => 'projects', :conditions => 'developers_projects.access_level = 1', :limit => 5).size
706 def test_order_on_join_table_with_include_and_limit
707 assert_equal
5, Developer
.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size
710 def test_eager_loading_with_order_on_joined_table_preloads
711 posts
= assert_queries(2) do
712 Post
.find(:all, :joins => :comments, :include => :author, :order => 'comments.id DESC')
714 assert_equal
posts(:eager_other), posts
[0]
715 assert_equal
authors(:mary), assert_no_queries
{ posts
[0].author
}
718 def test_eager_loading_with_conditions_on_joined_table_preloads
719 posts
= assert_queries(2) do
720 Post
.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
722 assert_equal
[posts(:welcome)], posts
723 assert_equal
authors(:david), assert_no_queries
{ posts
[0].author
}
725 posts
= assert_queries(2) do
726 Post
.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
728 assert_equal
[posts(:welcome)], posts
729 assert_equal
authors(:david), assert_no_queries
{ posts
[0].author
}
731 posts
= assert_queries(2) do
732 Post
.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'", :order => 'posts.id')
734 assert_equal
posts(:welcome, :thinking), posts
736 posts
= assert_queries(2) do
737 Post
.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id')
739 assert_equal
posts(:welcome, :thinking), posts
743 def test_eager_loading_with_conditions_on_string_joined_table_preloads
744 posts
= assert_queries(2) do
745 Post
.find(:all, :select => 'distinct posts.*', :include => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
747 assert_equal
[posts(:welcome)], posts
748 assert_equal
authors(:david), assert_no_queries
{ posts
[0].author
}
750 posts
= assert_queries(2) do
751 Post
.find(:all, :select => 'distinct posts.*', :include => :author, :joins => ["INNER JOIN comments on comments.post_id = posts.id"], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
753 assert_equal
[posts(:welcome)], posts
754 assert_equal
authors(:david), assert_no_queries
{ posts
[0].author
}
758 def test_eager_loading_with_select_on_joined_table_preloads
759 posts
= assert_queries(2) do
760 Post
.find(:all, :select => 'posts.*, authors.name as author_name', :include => :comments, :joins => :author, :order => 'posts.id')
762 assert_equal
'David', posts
[0].author_name
763 assert_equal
posts(:welcome).comments
, assert_no_queries
{ posts
[0].comments
}
766 def test_eager_loading_with_conditions_on_join_model_preloads
767 authors
= assert_queries(2) do
768 Author
.find(:all, :include => :author_address, :joins => :comments, :conditions => "posts.title like 'Welcome%'")
770 assert_equal
authors(:david), authors
[0]
771 assert_equal
author_addresses(:david_address), authors
[0].author_address
774 def test_preload_belongs_to_uses_exclusive_scope
775 people
= Person
.males
.find(:all, :include => :primary_contact)
776 assert_not_equal people
.length
, 0
777 people
.each
do |person
|
778 assert_no_queries
{assert_not_nil person
.primary_contact
}
779 assert_equal Person
.find(person
.id
).primary_contact
, person
.primary_contact
783 def test_preload_has_many_uses_exclusive_scope
784 people
= Person
.males
.find
:all, :include => :agents
785 people
.each
do |person
|
786 assert_equal Person
.find(person
.id
).agents
, person
.agents
790 def test_preload_has_many_using_primary_key
791 expected
= Firm
.find(:first).clients_using_primary_key
.to_a
792 firm
= Firm
.find
:first, :include => :clients_using_primary_key
794 assert_equal expected
, firm
.clients_using_primary_key
798 def test_include_has_many_using_primary_key
799 expected
= Firm
.find(1).clients_using_primary_key
.sort_by
&:name
800 firm
= Firm
.find
1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
802 assert_equal expected
, firm
.clients_using_primary_key
806 def test_preload_has_one_using_primary_key
807 expected
= Firm
.find(:first).account_using_primary_key
808 firm
= Firm
.find
:first, :include => :account_using_primary_key
810 assert_equal expected
, firm
.account_using_primary_key
814 def test_include_has_one_using_primary_key
815 expected
= Firm
.find(1).account_using_primary_key
816 firm
= Firm
.find(:all, :include => :account_using_primary_key, :order => 'accounts.id').detect
{|f
| f
.id
== 1}
818 assert_equal expected
, firm
.account_using_primary_key