Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add failing test case for preload_associations_lazily for association defined with scope #42

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions spec/ar_lazy_preload/preload_associations_lazily_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,35 @@

it "does not load association with scope" do
expect do
subject.flat_map do |u|
subject.each do |u|
u.posts.where(
created_at: ::Time.zone.at(0)..(::Time.zone.at(0) + 1)
).size
).map(&:id)
end
end.to_not raise_error
end

# SELECT "users".* FROM "users"
# SELECT "comments".* FROM "comments" WHERE "comments"."user_id" IN (...)
# AND "comments"."parent_comment_id" IS NULL
it "does not load association defined with scope" do
expect do
subject.each do |u|
u.thread_comments.map(&:id)
end
end.to make_database_queries(count: 2)
end

# SELECT "users".* FROM "users"
# SELECT "comments".* FROM "comments" WHERE "comments"."user_id" IN (...)
# AND "comments"."parent_comment_id" IS NULL
it "does not call COUNT(*) when association is already loaded" do
expect do
subject.each do |u|
u.thread_comments.map(&:id)
u.thread_comments.size
end
end.to make_database_queries(count: 2)
end
end
end
1 change: 1 addition & 0 deletions spec/helpers/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class User < ActiveRecord::Base
has_many :posts
has_many :comments
has_many :comments_on_posts, through: :posts, source: :comments
has_many :thread_comments, -> { threads }, class_name: "Comment"

def vote_for(voteable)
voteable.votes.create(user: self)
Expand Down