Skip to content

Commit

Permalink
Add README section for returns info of import
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonwelch committed Nov 19, 2018
1 parent d8f7737 commit da80cd8
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The gem provides the following high-level features:
* [Duplicate Key Ignore](#duplicate-key-ignore)
* [Duplicate Key Update](#duplicate-key-update)
* [Uniqueness Validation](#uniqueness-validation)
* [Return Info](#return-info)
* [Counter Cache](#counter-cache)
* [ActiveRecord Timestamps](#activerecord-timestamps)
* [Callbacks](#callbacks)
Expand Down Expand Up @@ -358,6 +359,34 @@ By default, `activerecord-import` will not validate for uniquness when importing
Book.import books, validate_uniqueness: true
```

### Return Info

The `import` method returns a `Result` object that responds to `failed_instances` and `num_inserts`. Additionally, for users of Postgres, there will be two arrays `ids` and `results` that can be accessed`.

```ruby
articles = [
Article.new(author_id: 1, title: 'First Article', content: 'This is the first article'),
Article.new(author_id: 2, title: 'Second Article', content: ''),
Article.new(author_id: 3, content: '')
]

demo = Article.import(articles), returning: :title # => #<struct ActiveRecord::Import::Result

demo.failed_instances
=> [#<Article id: 3, author_id: 3, title: nil, content: "", created_at: nil, updated_at: nil>]

demo.num_inserts
=> 1,

demo.ids
=> ["1", "2"] # for Postgres
=> [] # for other DBs

demo.results
=> ["First Article", "Second Article"] # for Postgres
=> [] for other DBs
```

### Counter Cache

When running `import`, `activerecord-import` does not automatically update counter cache columns. To update these columns, you will need to do one of the following:
Expand Down

0 comments on commit da80cd8

Please # to comment.