README.md in batch-loader-1.0.4 vs README.md in batch-loader-1.1.0
- old
+ new
@@ -15,10 +15,11 @@
* [Why?](#why)
* [Basic example](#basic-example)
* [How it works](#how-it-works)
* [RESTful API example](#restful-api-example)
* [GraphQL example](#graphql-example)
+ * [Loading multiple items](#loading-multiple-items)
* [Caching](#caching)
* [Installation](#installation)
* [Implementation details](#implementation-details)
* [Development](#development)
* [Contributing](#contributing)
@@ -276,9 +277,29 @@
use BatchLoader::GraphQL
end
```
That's it.
+
+### Loading multiple items
+
+For batches where there is no item in response to a call, we normally return `nil`. However, you can use `:default_value` to return something else instead:
+
+```ruby
+BatchLoader.for(post.user_id).batch(default_value: NullUser.new) do |user_ids, loader|
+ User.where(id: user_ids).each { |user| loader.call(user.id, user) }
+end
+```
+
+For batches where the value is some kind of collection, such as an Array or Hash, `loader` also supports being called with a block, which yields the _current_ value, and returns the _next_ value. This is extremely useful for 1:Many relationships:
+
+```ruby
+BatchLoader.for(user.id).batch(default_value: []) do |comment_ids, loader|
+ Comment.where(user_id: user_ids).each do |comment|
+ loader.call(user.id) { |memo| memo << comment }
+ end
+end
+```
### Caching
By default `BatchLoader` caches the loaded values. You can test it by running something like: