Commit 8419a973 authored by Evan Read's avatar Evan Read

Merge branch 'russell/fix-code-sample-and-style' into 'master'

Fixed syntax issue in code sample

See merge request gitlab-org/gitlab!45730
parents 4149a6d4 664dcd97
......@@ -2,8 +2,8 @@
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/32921) in GitLab 13.5.
Frequently, we will want to update multiple objects with new values for one
or more columns. The obvious way to do this is using `Relation#update_all`:
There's often a need to update multiple objects with new values for one
or more columns. One method of doing this is using `Relation#update_all`:
```ruby
user.issues.open.update_all(due_date: 7.days.from_now) # (1)
......@@ -28,11 +28,11 @@ update issues
where id = obj_id
```
The bad news: There is no way to express this in ActiveRecord or even dropping
down to ARel - the `UpdateManager` just does not support `update from`, so this
The bad news: there is no way to express this in ActiveRecord or even dropping
down to ARel. The `UpdateManager` does not support `update from`, so this
is not expressible.
The good news: We supply an abstraction to help you generate these kinds of
The good news: we supply an abstraction to help you generate these kinds of
updates, called `Gitlab::Database::BulkUpdate`. This constructs queries such as the
above, and uses binding parameters to avoid SQL injection.
......@@ -44,7 +44,7 @@ To use this, we need:
- a mapping from object/ID to the new values to set for that object
- a way to determine the table for each object
So for example, we can express the query above as:
For example, we can express the query above as:
```ruby
issue_a = Issue.find(..)
......@@ -87,7 +87,7 @@ objects = Foo.from_union([
])
# At this point, all the objects are instances of Foo, even the ones from the
# Bar table
mapping = objects.to_h { |obj| [obj, bazzes[obj.id] }
mapping = objects.to_h { |obj| [obj, bazzes[obj.id]] }
# Issues at most 2 queries
::Gitlab::Database::BulkUpdate.execute(%i[baz], mapping) do |obj|
......@@ -100,4 +100,4 @@ end
Note that this is a **very low level** tool, and operates on the raw column
values. Enumerations and state fields must be translated into their underlying
representations, for example, and nested associations are not supported. No
validations or hooks will be called.
validations or hooks are called.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment