Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added ActiveRecord::Relation#none! method #7750

Merged
merged 2 commits into from

7 participants

@xuanxu

The none bang method in AR::Relation to modify objects in place was missing.

@fxn
Owner
@josevalim
Owner

:-1: we should not promote mutating relations in place.

@xuanxu

@josevalim ¿? but every other query method has already a paired method to modify relations in place (from!, where! select!, extending!, having!, order!....)

@frodsan

Can you rebase it?

@xuanxu

Rebased

@jonleighton
Collaborator

@josevalim I agree we shouldn't really promote it. I added mutation mainly for internal use plus for optimisation of bottleneck areas in app code. But we should probably add this simply for consistency, wdyt?

@frodsan

:+1: for consistency. If we shouldn't really promote it, why we have them in the public API? Maybe we can :nodoc: them.

@rafaelfranca

:+1: for consistency

@carlosantoniodasilva

I'm also :+1: for consistency. @xuanxu github says it cannot be cleanly merged anymore, probably the changelog, can you rebase it please?

@xuanxu

@carlosantoniodasilva yep, it was the changelog. Rebased now.

@rafaelfranca rafaelfranca merged commit 852e376 into rails:master
@rafaelfranca

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
5 activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
+* Added `#none!` method for mutating `ActiveRecord::Relation` objects to a NullRelation.
+ It acts like `#none` but modifies relation in place.
+
+ *Juanjo Bazán*
+
* Fix bug where `update_columns` and `update_column` would not let you update the primary key column.
*Henrik Nyh*
View
5 activerecord/lib/active_record/relation/query_methods.rb
@@ -496,6 +496,11 @@ def none
extending(NullRelation)
end
+ # Like #none, but modifies relation in place.
+ def none!
+ extending!(NullRelation)
+ end
+
# Sets readonly attributes for the returned relation. If value is
# true (default), attempting to update a record will result in an error.
#
View
6 activerecord/test/cases/relation_test.rb
@@ -256,5 +256,11 @@ def relation
test 'merge with a proc' do
assert_equal [:foo], relation.merge(-> { where(:foo) }).where_values
end
+
+ test 'none!' do
+ assert relation.none!.equal?(relation)
+ assert_equal [NullRelation], relation.extending_values
+ assert relation.is_a?(NullRelation)
+ end
end
end
View
9 activerecord/test/cases/relations_test.rb
@@ -1391,6 +1391,15 @@ def test_presence
end
end
+ test "loaded relations cannot be mutated by extending!" do
+ relation = Post.all
+ relation.to_a
+
+ assert_raises(ActiveRecord::ImmutableRelation) do
+ relation.extending! Module.new
+ end
+ end
+
test "relations show the records in #inspect" do
relation = Post.limit(2)
assert_equal "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>", relation.inspect
Something went wrong with that request. Please try again.