Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

ActiveRecord::Relation#except and #only loses scope extensions #231

Closed
wants to merge 1 commit into from

2 participants

@iain

Repeating what I've said in Lighthouse ticket 6598

Given a scope with extensions
When I use the except to remove a part from the relation
Then the extensions are gone, because except returns a brand new relation object.

This means the following doesn't work (when using Kaminari, which uses extensions):

Post.page(2).except(:order).current_page

Because the current_page method was added by the page scope.

This issue is present in at least 3.0.5 and master.

The supplied patch will apply the extensions again after using except and only.

There is some duplication going on in both methods. A private method could certainly be extracted (although I'm clueless on a name).

@tenderlove
Owner

Merged. Thank you!

@tenderlove tenderlove closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 19, 2011
  1. @iain
This page is out of date. Refresh to see the latest.
View
6 activerecord/lib/active_record/relation/spawn_methods.rb
@@ -79,6 +79,9 @@ def except(*skips)
result.send(:"#{method}_value=", send(:"#{method}_value"))
end
+ # Apply scope extension modules
+ result.send(:apply_modules, extensions)
+
result
end
@@ -100,6 +103,9 @@ def only(*onlies)
result.send(:"#{method}_value=", send(:"#{method}_value"))
end
+ # Apply scope extension modules
+ result.send(:apply_modules, extensions)
+
result
end
View
8 activerecord/test/cases/relations_test.rb
@@ -797,6 +797,10 @@ def test_except
assert_equal Post.all, all_posts.all
end
+ def test_extensions_with_except
+ assert_equal 2, Topic.named_extension.order(:author_name).except(:order).two
+ end
+
def test_only
relation = Post.where(:author_id => 1).order('id ASC').limit(1)
assert_equal [posts(:welcome)], relation.all
@@ -808,6 +812,10 @@ def test_only
assert_equal Post.limit(1).all.first, all_posts.first
end
+ def test_extensions_with_only
+ assert_equal 2, Topic.named_extension.order(:author_name).only(:order).two
+ end
+
def test_anonymous_extension
relation = Post.where(:author_id => 1).order('id ASC').extending do
def author
Something went wrong with that request. Please try again.