Skip to content

Commit

Permalink
Pass block to ActiveRecord::Relation#exec_queries to fix inverse_of i…
Browse files Browse the repository at this point in the history
…n Rails 5.x. Fixes #42.
  • Loading branch information
jturkel committed Apr 27, 2017
1 parent 70df5a8 commit e824f04
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

### 0.0.12 (unreleased)
*
* Fix [issue 42](https://github.com/salsify/goldiloader/issues/42) - inverse_of now work properly in Rails 5.x.

### 0.0.11
* Fix [issue 34](https://github.com/salsify/goldiloader/issues/34) - HABTM associations now honor
Expand Down
6 changes: 3 additions & 3 deletions lib/goldiloader/active_record_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def reload(*)

ActiveRecord::Relation.class_eval do

def exec_queries_with_auto_include
return exec_queries_without_auto_include if loaded?
def exec_queries_with_auto_include(&block)
return exec_queries_without_auto_include(&block) if loaded?

models = exec_queries_without_auto_include
models = exec_queries_without_auto_include(&block)
Goldiloader::AutoIncludeContext.register_models(models, eager_load_values)
models
end
Expand Down
1 change: 1 addition & 0 deletions spec/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class PostTag < ActiveRecord::Base

class Blog < ActiveRecord::Base
has_many :posts
has_many :posts_with_inverse_of, class_name: 'Post', inverse_of: :blog
has_many :posts_without_auto_include, auto_include: false, class_name: 'Post'
has_many :posts_fully_load, fully_load: true, class_name: 'Post'

Expand Down
15 changes: 15 additions & 0 deletions spec/goldiloader/goldiloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@
expect(users.second.posts).to eq Post.where(author_id: author2.id)
end

it "sets inverse associations properly" do
blogs = Blog.order(:name).to_a

# Force the first blog's posts to load
blogs.first.posts_with_inverse_of.to_a

blogs.each do |blog|
expect(blog.association(:posts_with_inverse_of)).to be_loaded
blog.posts_with_inverse_of.each do |post|
expect(post.association(:blog)).to be_loaded
expect(post.blog.object_id).to eq(blog.object_id)
end
end
end

it "only auto eager loads associations loaded through the same path" do
root_tags = Tag.where(parent_id: nil).order(:name).to_a
root_tags.first.children.to_a
Expand Down

0 comments on commit e824f04

Please sign in to comment.