Use ActiveRecord to manage connections

Insert data by using ActiveRecord to manage opening and closing a connection to Cloud SQL for PostgreSQL.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample


To authenticate to Cloud SQL for PostgreSQL, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

@vote = candidate: candidate

# ActiveRecord creates and executes your SQL and automatically
# handles the opening and closing of the database connection.
  render json: "Vote successfully cast for \"#{@vote.candidate}\" at #{@vote.time_cast} PST!"
  render json: @vote.errors, status: :unprocessable_entity

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.