Manage connections by using ActiveRecord

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

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample

Ruby

@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