Stay organized with collectionsSave and categorize content based on your preferences.
RubyActive Recordis an Object-Relational
Mapping (ORM) library bundled withRuby on Rails.
Active Record provides an abstraction over the underlying database and
includes capabilities such as automatically generating schema changes and
managing schema version history.
TheSpanner support for Active RecordenablesActive Recordusers to use Spanner
databases. With this support, Ruby applications can take advantage of
Spanner's high availability and external consistency at scale through
an ORM.
Setting up the Spanner support for Active Record
To setup the Spanner support for Active Record in your application, edit theGemfileof your Rails application and add theactiverecord-spanner-adaptergem.
gem 'activerecord-spanner-adapter'
Next, run bundle to install the gem.
bundle install
As authentication for the Spanner support for Active Record, theservice account JSON credentialsfile location should be
provided in theGOOGLE_APPLICATION_CREDENTIALSenvironment variable.
Otherwise, the Spanner support for Active Record can also use the default credentials
set in the Google Cloud SDKgcloudapplication.
Using the Spanner support for Active Record
For more information about the available features, limitations of the
Spanner support for Active Record, recommendations on how to use it, and for code
samples, please consult thereference documentationon GitHub.
What's next
Checkout thecode exampleson how to use
the Spanner support for Active Record.
View the repository for the Spanner support for Active Record onGitHub.
File aGitHub issueto report a bug or ask
a question about the Spanner support for Active Record.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Using Active Record with Spanner\n\nRuby [Active Record](https://guides.rubyonrails.org/active_record_basics.html) is an Object-Relational\nMapping (ORM) library bundled with [Ruby on Rails](https://rubyonrails.org/).\nActive Record provides an abstraction over the underlying database and\nincludes capabilities such as automatically generating schema changes and\nmanaging schema version history.\n\nThe [Spanner support for Active Record](https://github.com/googleapis/ruby-spanner-activerecord)\nenables [Active Record](https://guides.rubyonrails.org/active_record_basics.html) users to use Spanner\ndatabases. With this support, Ruby applications can take advantage of\nSpanner's high availability and external consistency at scale through\nan ORM.\n\n\n| **PostgreSQL interface note:** The [PostgreSQL interface for Spanner](/spanner/docs/postgresql-interface) doesn't support Active Record.\n\n\u003cbr /\u003e\n\nSetting up the Spanner support for Active Record\n------------------------------------------------\n\nTo setup the Spanner support for Active Record in your application, edit the\n`Gemfile` of your Rails application and add the\n[activerecord-spanner-adapter](https://rubygems.org/gems/activerecord-spanner-adapter)\ngem. \n\n gem 'activerecord-spanner-adapter'\n\nNext, run bundle to install the gem. \n\n bundle install\n\nAs authentication for the Spanner support for Active Record, the\n[service account JSON credentials](/docs/authentication/getting-started) file location should be\nprovided in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.\nOtherwise, the Spanner support for Active Record can also use the default credentials\nset in the Google Cloud SDK `gcloud` application.\n\nUsing the Spanner support for Active Record\n-------------------------------------------\n\nFor more information about the available features, limitations of the\nSpanner support for Active Record, recommendations on how to use it, and for code\nsamples, please consult the\n[reference documentation](https://github.com/googleapis/ruby-spanner-activerecord#readme)\non GitHub.\n\nWhat's next\n-----------\n\n- Checkout the [code examples](https://github.com/googleapis/ruby-spanner-activerecord#examples) on how to use the Spanner support for Active Record.\n- View the repository for the Spanner support for Active Record on [GitHub](https://github.com/googleapis/ruby-spanner-activerecord).\n- File a [GitHub issue](https://github.com/googleapis/ruby-spanner-activerecord/issues) to report a bug or ask a question about the Spanner support for Active Record.\n- Learn more about [Active Record](https://guides.rubyonrails.org/active_record_basics.html).\n- Learn more about [Ruby Gems](https://rubygems.org/).\n- Learn about authorization and authentication credentials in [Getting started with authentication](/docs/authentication/getting-started)."]]