Skip to content

Commit

Permalink
Add Relation#delete [Pratik Naik, Emilio Tagua]
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo committed Dec 31, 2009
1 parent ea41a75 commit d5f9173
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
Expand Up @@ -73,7 +73,7 @@ def delete_records(records)
relation = arel_table(@reflection.options[:join_table]) relation = arel_table(@reflection.options[:join_table])
relation.where(relation[@reflection.primary_key_name].eq(@owner.id). relation.where(relation[@reflection.primary_key_name].eq(@owner.id).
and(Arel::Predicates::In.new(relation[@reflection.association_foreign_key], records.map(&:id))) and(Arel::Predicates::In.new(relation[@reflection.association_foreign_key], records.map(&:id)))
).delete ).delete_all
end end
end end


Expand Down
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -815,8 +815,8 @@ def update(id, attributes)
# #
# # Delete multiple rows # # Delete multiple rows
# Todo.delete([2,3,4]) # Todo.delete([2,3,4])
def delete(id) def delete(id_or_array)
delete_all([ "#{connection.quote_column_name(primary_key)} IN (?)", id ]) arel_table.where(construct_conditions(nil, scope(:find))).delete(id_or_array)
end end


# Destroy an object (or multiple objects) that has the given id, the object is instantiated first, # Destroy an object (or multiple objects) that has the given id, the object is instantiated first,
Expand Down Expand Up @@ -2323,7 +2323,7 @@ def delete
# be made (since they can't be persisted). # be made (since they can't be persisted).
def destroy def destroy
unless new_record? unless new_record?
self.class.arel_table.where(self.class.arel_table[self.class.primary_key].eq(id)).delete self.class.arel_table.where(self.class.arel_table[self.class.primary_key].eq(id)).delete_all
end end


@destroyed = true @destroyed = true
Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/relation.rb
Expand Up @@ -109,6 +109,10 @@ def delete_all
@relation.delete.tap { reset } @relation.delete.tap { reset }
end end


def delete(id_or_array)
where(@klass.primary_key => id_or_array).delete_all
end

def loaded? def loaded?
@loaded @loaded
end end
Expand Down

0 comments on commit d5f9173

Please sign in to comment.