---
title: "Rails 7.1 allows using aliased attributes with insert_all/upsert_all"
description:
  "Rails 7.1 allows using aliased attributes with insert_all/upsert_all"
canonical_url: "https://www.bigbinary.com/blog/rails-7-1-allows-using-aliased-attributes-with-insert_all-upsert_all"
markdown_url: "https://www.bigbinary.com/blog/rails-7-1-allows-using-aliased-attributes-with-insert_all-upsert_all.md"
---

# Rails 7.1 allows using aliased attributes with insert_all/upsert_all

Rails 7.1 allows using aliased attributes with insert_all/upsert_all

- Author: Aditya Bhutani
- Published: January 11, 2023
- Categories: Rails, Rails 7

Before Rails 6 we had update_all and delete_all. Rails 6 added insert_all and
upsert_all.

[insert_all](https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert_all)
: This method can insert multiple records with a single SQL INSERT statement.

[upsert_all](https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-upsert_all)
: This method updates the records if they exist or inserts them into the
database with a single SQL INSERT statement.

[alias_attribute](https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods/ClassMethods.html#method-i-alias_attribute)
: Allows you to make aliases for attributes, which include a getter, a setter,
and a predicate.

Rails 7.1 allows the use of aliased attributes with `insert_all` and
`upsert_all`. Previously whenever we added an alias for an attribute, we
couldn't use it for insert_all and upsert_all.

```ruby
class User < ApplicationRecord
  # database column is `name`. `full_name` is the alias.
  alias_attribute :full_name, :name
end
```

### Before Rails 7.1

```ruby
# rails console
> User.insert_all [{ full_name: "John Doe" }]
=> # unknown attribute 'full_name' for User. (ActiveModel::UnknownAttributeError)
```

### After Rails 7.1

```ruby
# rails console
> User.insert_all [{ full_name: "Jane Doe" }]
=> # User Insert

> User.last
=> #<User id: 6, name: "Jane Doe", created_at: Mon, 21 Nov 2022 18:07:11.349000000 UTC +00:00, updated_at: Mon, 21 Nov 2022 18:07:11.349000000 UTC +00:00>
```

Now we can use the alias attribute with `insert_all` and `upsert_all`.

Please check out this [pull request](https://github.com/rails/rails/pull/45036)
for more details.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-7-1-allows-using-aliased-attributes-with-insert_all-upsert_all)
