Manage connections by using ActiveRecord

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

Explore further

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

Code sample

Ruby

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

@vote = Vote.new candidate: candidate

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

What's next

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