mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add Relation#delete_all
This commit is contained in:
parent
f79caa49fb
commit
54b80c7361
3 changed files with 30 additions and 0 deletions
|
@ -1,5 +1,9 @@
|
|||
*Edge*
|
||||
|
||||
* Add Relation#delete_all. [Pratik Naik]
|
||||
|
||||
Item.where(:colour => 'red').delete_all
|
||||
|
||||
* Add Model.having and Relation#having. [Pratik Naik]
|
||||
|
||||
Developer.group("salary").having("sum(salary) > 10000").select("salary")
|
||||
|
|
|
@ -134,6 +134,7 @@ module ActiveRecord
|
|||
|
||||
if [String, Hash, Array].include?(args.first.class)
|
||||
conditions = @klass.send(:merge_conditions, args.size > 1 ? Array.wrap(args) : args.first)
|
||||
conditions = Arel::SqlLiteral.new(conditions) if conditions
|
||||
else
|
||||
conditions = args.first
|
||||
end
|
||||
|
@ -223,6 +224,11 @@ module ActiveRecord
|
|||
reset
|
||||
end
|
||||
|
||||
def delete_all
|
||||
@relation.delete
|
||||
reset
|
||||
end
|
||||
|
||||
def loaded?
|
||||
@loaded
|
||||
end
|
||||
|
|
|
@ -329,6 +329,26 @@ class RelationTest < ActiveRecord::TestCase
|
|||
assert davids.loaded?
|
||||
end
|
||||
|
||||
def test_delete_all
|
||||
davids = Author.where(:name => 'David')
|
||||
|
||||
assert_difference('Author.count', -1) { davids.delete_all }
|
||||
assert ! davids.loaded?
|
||||
end
|
||||
|
||||
def test_delete_all_loaded
|
||||
davids = Author.where(:name => 'David')
|
||||
|
||||
# Force load
|
||||
assert_equal [authors(:david)], davids.to_a
|
||||
assert davids.loaded?
|
||||
|
||||
assert_difference('Author.count', -1) { davids.delete_all }
|
||||
|
||||
assert_equal [], davids.to_a
|
||||
assert davids.loaded?
|
||||
end
|
||||
|
||||
def test_relation_merging
|
||||
devs = Developer.where("salary >= 80000") & Developer.limit(2) & Developer.order('id ASC').where("id < 3")
|
||||
assert_equal [developers(:david), developers(:jamis)], devs.to_a
|
||||
|
|
Loading…
Reference in a new issue