{"id":10249,"date":"2025-12-22T20:01:49","date_gmt":"2025-12-23T04:01:49","guid":{"rendered":"https:\/\/www.couchbase.com\/developers\/tutorials\/quickstart-ruby-on-rails\/"},"modified":"2025-12-22T20:01:49","modified_gmt":"2025-12-23T04:01:49","slug":"quickstart-ruby-on-rails","status":"publish","type":"tutorials","link":"https:\/\/www.couchbase.com\/de\/developers\/tutorials\/quickstart-ruby-on-rails\/","title":{"rendered":"Quickstart in Couchbase with Ruby on Rails"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this tutorial, you will learn how to connect to a Couchbase Capella cluster to create, read, update, and delete documents and how to write simple parametrized SQL++ queries.<\/p>\n\n\n\n<h3 id=\"h-prerequisites\" class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To run this prebuilt project, you will need:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\n<p><a href=\"https:\/\/www.couchbase.com\/products\/capella\/\">Couchbase Capella<\/a> cluster with <a href=\"https:\/\/docs.couchbase.com\/ruby-sdk\/current\/ref\/travel-app-data-model.html\">travel-sample<\/a> bucket loaded.<\/p>\n<ul>\n<li>To run this tutorial using a self managed Couchbase cluster, please refer to the <a href=\"#running-self-managed-couchbase-cluster\">appendix<\/a>.<\/li>\n<\/ul>\n<\/li>\n\n\n<li>\n<p><a href=\"https:\/\/www.ruby-lang.org\/en\/documentation\/installation\/\">Ruby<\/a> installed on your machine.<\/p>\n<ul>\n<li>Ensure that the Ruby version is <a href=\"https:\/\/docs.couchbase.com\/ruby-sdk\/current\/project-docs\/compatibility.html#ruby-version-compatibility\">compatible<\/a> with the Couchbase SDK.<\/li>\n<\/ul>\n<\/li>\n\n\n<li>\n<p>Loading Travel Sample Bucket\nIf travel-sample is not loaded in your Capella cluster, you can load it by following the instructions for your Capella Cluster:<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.couchbase.com\/cloud\/clusters\/data-service\/import-data-documents.html#import-sample-data\">Load travel-sample bucket in Couchbase Capella<\/a><\/li>\n<\/ul>\n<\/li>\n\n<\/ul>\n\n\n\n<h4 id=\"h-couchbase-capella-configuration\" class=\"wp-block-heading\">Couchbase Capella Configuration<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When running Couchbase using <a href=\"https:\/\/cloud.couchbase.com\/\">Capella<\/a>, the following prerequisites need to be met.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The application requires the travel-sample bucket to be <a href=\"https:\/\/docs.couchbase.com\/cloud\/clusters\/data-service\/import-data-documents.html#import-sample-data\">loaded<\/a> in the cluster from the Capella UI.<\/li>\n\n\n<li>Create the <a href=\"https:\/\/docs.couchbase.com\/cloud\/clusters\/manage-database-users.html\">database credentials<\/a> to access the travel-sample bucket (Read and Write) used in the application.<\/li>\n\n\n<li><a href=\"https:\/\/docs.couchbase.com\/cloud\/clusters\/allow-ip-address.html\">Allow access<\/a> to the Cluster from the IP on which the application is running.<\/li>\n\n<\/ul>\n\n\n\n<h3 id=\"h-app-setup\" class=\"wp-block-heading\">App Setup<\/h3>\n\n\n\n<h4 id=\"h-cloning-repo\" class=\"wp-block-heading\">Cloning Repo<\/h4>\n\n\n\n<pre class=\"wp-block-code language-shell\"><code>git clone https:\/\/github.com\/couchbase-examples\/ruby-on-rails-quickstart.git\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-install-dependencies\" class=\"wp-block-heading\">Install Dependencies<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The dependencies for the application are specified in the <code>Gemfile<\/code> file in the source folder. Dependencies can be installed through <code>bundler<\/code> the default package manager for Ruby.<\/p>\n\n\n\n<pre class=\"wp-block-code language-shell\"><code>cd ruby-on-rails-quickstart\ngem install bundler\nbundle install\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Refer to the <a href=\"https:\/\/github.com\/couchbase\/couchbase-ruby-client#installation\">instructions in the SDK<\/a> for more info.<\/p>\n<\/blockquote>\n\n\n\n<h4 id=\"h-setup-database-configuration\" class=\"wp-block-heading\">Setup Database Configuration<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">All configuration for communication with the database is fetched from the environment variables. As a security best practice, this quickstart reads the environment variables from a local file, <code>dev.env<\/code> in the source folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a copy of <code>dev.env.example<\/code> file &amp; rename it to <code>dev.env<\/code> &amp; add the values for the Couchbase cluster.<\/p>\n\n\n\n<pre class=\"wp-block-code language-sh\"><code>DB_CONN_STR=&lt;connection_string&gt;\nDB_USERNAME=&lt;user_with_read_write_permission_to_travel-sample_bucket&gt;\nDB_PASSWORD=&lt;password_for_user&gt;\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Note: The connection string expects the <code>couchbases:\/\/<\/code> or <code>couchbase:\/\/<\/code> part.<\/p>\n<\/blockquote>\n\n\n\n<h3 id=\"h-running-the-application\" class=\"wp-block-heading\">Running the Application<\/h3>\n\n\n\n<h4 id=\"h-directly-on-local-machine\" class=\"wp-block-heading\">Directly on Local Machine<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">At this point, we have installed the dependencies, loaded the travel-sample data and configured the application with the credentials. The application is now ready and you can run it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The application will run on port 3000 of your local machine (<a href=\"http:\/\/localhost:3000\">http:\/\/localhost:3000<\/a>). You will find the Swagger UI to try the API endpoints.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Swagger Page will be available at <a href=\"http:\/\/localhost:3000\/api-docs\">http:\/\/localhost:3000\/api-docs<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-shell\"><code>rails s\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-using-docker\" class=\"wp-block-heading\">Using Docker<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you prefer to run this quick start using Docker, we have provided the Dockerfile which you can use to build the image and run the API as a container.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Build the Docker image<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-sh\"><code>docker build -t ruby-on-rails-quickstart .\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Run the Docker image<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-sh\"><code>docker run --env-file dev.env -p 3000:3000 ruby-on-rails-quickstart\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Note: The <code>.env<\/code> file has the connection information to connect to your Capella cluster. With the <code>--env-file<\/code> flag, Docker injects those environment variables to the container, overriding any default values defined in the application code.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Once the app is up and running, open <a href=\"http:\/\/localhost:3000\/api-docs\">http:\/\/localhost:3000\/api-docs<\/a> to test the APIs.<\/p>\n\n\n\n<h4 id=\"h-verifying-the-application\" class=\"wp-block-heading\">Verifying the Application<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Once the application starts, you can see the details of the application on the logs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"app_startup.png\" alt=\"Application Startup\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The application will run on port 3000 of your local machine (<a href=\"http:\/\/localhost:3000\">http:\/\/localhost:3000<\/a>). You will find the interactive Swagger documentation of the API if you go to the URL in your browser. Swagger documentation is used in this demo to showcase the different API end points and how they can be invoked. More details on the Swagger documentation can be found in the <a href=\"#swagger-documentation\">appendix<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"swagger_documentation.png\" alt=\"Swagger Documentation\"><\/p>\n\n\n\n<h3 id=\"h-health-check-endpoint\" class=\"wp-block-heading\">Health Check Endpoint<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The application provides a health check endpoint for monitoring application health and Couchbase connectivity:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Endpoint:<\/strong> <code>GET \/api\/v1\/health<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Response (200 OK):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{\n  &quot;status&quot;: &quot;healthy&quot;,\n  &quot;timestamp&quot;: &quot;2025-12-02T10:00:00Z&quot;,\n  &quot;services&quot;: {\n    &quot;couchbase&quot;: {\n      &quot;status&quot;: &quot;up&quot;,\n      &quot;message&quot;: &quot;Connected to Couchbase bucket: travel-sample&quot;\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Response (503 Service Unavailable):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{\n  &quot;status&quot;: &quot;unhealthy&quot;,\n  &quot;timestamp&quot;: &quot;2025-12-02T10:00:00Z&quot;,\n  &quot;services&quot;: {\n    &quot;couchbase&quot;: {\n      &quot;status&quot;: &quot;down&quot;,\n      &quot;message&quot;: &quot;Connection error details&quot;\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Usage:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Monitor application health in production environments<\/li>\n\n\n<li>Use for container readiness\/liveness probes in Kubernetes<\/li>\n\n\n<li>Verify Couchbase connectivity before running integration tests<\/li>\n\n\n<li>Include in uptime monitoring systems<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You can test the health check endpoint by navigating to <code>http:\/\/localhost:3000\/api\/v1\/health<\/code> in your browser or using curl:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>curl http:\/\/localhost:3000\/api\/v1\/health\n<\/code><\/pre>\n\n\n\n<h3 id=\"h-data-model\" class=\"wp-block-heading\">Data Model<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For this tutorial, we use three collections, <code>airport<\/code>, <code>airline<\/code> and <code>route<\/code> that contain sample airports, airlines and airline routes respectively. The route collection connects the airports and airlines as seen in the figure below. We use these connections in the quickstart to generate airports that are directly connected and airlines connecting to a destination airport. Note that these are just examples to highlight how you can use SQL++ queries to join the collections.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"travel_sample_data_model.png\" alt=\"img\"><\/p>\n\n\n\n<h3 id=\"h-code-structure\" class=\"wp-block-heading\">Code Structure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To begin this tutorial, clone the repo and open it up in the IDE of your choice. Now you can learn about how to create, read, update and delete documents in Couchbase Server.<\/p>\n\n\n\n<h4 id=\"h-code-layout\" class=\"wp-block-heading\">Code Layout<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>.\n\u251c\u2500\u2500 app\n\u2502   \u251c\u2500\u2500 controllers\n\u2502   \u2502   \u251c\u2500\u2500 api\n\u2502   \u2502   \u2502   \u2514\u2500\u2500 v1\n\u2502   \u2502   \u2502       \u251c\u2500\u2500 airlines_controller.rb\n\u2502   \u2502   \u2502       \u251c\u2500\u2500 airports_controller.rb\n\u2502   \u2502   \u2502       \u251c\u2500\u2500 health_controller.rb\n\u2502   \u2502   \u2502       \u251c\u2500\u2500 hotels_controller.rb\n\u2502   \u2502   \u2502       \u2514\u2500\u2500 routes_controller.rb\n\u2502   \u2502   \u251c\u2500\u2500 application_controller.rb\n\u2502   \u2502   \u2514\u2500\u2500 concerns\n\u2502   \u251c\u2500\u2500 models\n\u2502   \u2502   \u251c\u2500\u2500 airline.rb\n\u2502   \u2502   \u251c\u2500\u2500 airport.rb\n\u2502   \u2502   \u251c\u2500\u2500 application_record.rb\n\u2502   \u2502   \u251c\u2500\u2500 concerns\n\u2502   \u2502   \u251c\u2500\u2500 hotel.rb\n\u2502   \u2502   \u2514\u2500\u2500 route.rb\n\u2502   \u2514\u2500\u2500 views\n\u2502       \u2514\u2500\u2500 layouts\n\u2502           \u251c\u2500\u2500 application.html.erb\n\u2502           \u251c\u2500\u2500 mailer.html.erb\n\u2502           \u2514\u2500\u2500 mailer.text.erb\n\u251c\u2500\u2500 config\n\u2502   \u251c\u2500\u2500 importmap.rb\n\u2502   \u251c\u2500\u2500 initializers\n\u2502   \u2502   \u251c\u2500\u2500 couchbase.rb\n\u2502   \u2502   \u251c\u2500\u2500 rswag_api.rb\n\u2502   \u2502   \u2514\u2500\u2500 rswag_ui.rb\n\u2502   \u251c\u2500\u2500 routes.rb\n\u251c\u2500\u2500 config.ru\n\u251c\u2500\u2500 db\n\u2502   \u2514\u2500\u2500 seeds.rb\n\u251c\u2500\u2500 dev.env\n\u251c\u2500\u2500 dev.env.example\n\u251c\u2500\u2500 Dockerfile\n\u251c\u2500\u2500 Gemfile\n\u251c\u2500\u2500 Gemfile.lock\n\u251c\u2500\u2500 spec\n\u2502   \u251c\u2500\u2500 rails_helper.rb\n\u2502   \u251c\u2500\u2500 requests\n\u2502   \u2502   \u251c\u2500\u2500 api\n\u2502   \u2502   \u2502   \u2514\u2500\u2500 v1\n\u2502   \u2502   \u2502       \u251c\u2500\u2500 airlines_spec.rb\n\u2502   \u2502   \u2502       \u251c\u2500\u2500 airports_spec.rb\n\u2502   \u2502   \u2502       \u251c\u2500\u2500 hotels_spec.rb\n\u2502   \u2502   \u2502       \u2514\u2500\u2500 routes_spec.rb\n\u2502   \u2502   \u2514\u2500\u2500 swagger\n\u2502   \u2502       \u251c\u2500\u2500 airlines_spec.rb\n\u2502   \u2502       \u251c\u2500\u2500 airports_spec.rb\n\u2502   \u2502       \u251c\u2500\u2500 hotels_spec.rb\n\u2502   \u2502       \u2514\u2500\u2500 routes_spec.rb\n\u2502   \u251c\u2500\u2500 spec_helper.rb\n\u2502   \u2514\u2500\u2500 swagger_helper.rb\n\u251c\u2500\u2500 swagger\n\u2502   \u2514\u2500\u2500 v1\n\u2502       \u2514\u2500\u2500 swagger.yaml\n\u251c\u2500\u2500 test\n\u2502   \u251c\u2500\u2500 channels\n\u2502   \u2502   \u2514\u2500\u2500 application_cable\n\u2502   \u2502       \u2514\u2500\u2500 connection_test.rb\n\u2502   \u251c\u2500\u2500 controllers\n\u2502   \u251c\u2500\u2500 models\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The API endpoints are organized by entity (collection) with separate controller files in the <code>app\/controllers\/api\/v1\/<\/code> folder. The application uses two types of tests:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Integration tests<\/strong> in <code>spec\/requests\/api\/v1\/<\/code> verify actual API functionality<\/li>\n\n\n<li><strong>Swagger documentation specs<\/strong> in <code>spec\/requests\/swagger\/<\/code> generate OpenAPI documentation<\/li>\n\n\n<li>The generated <code>swagger.yaml<\/code> file appears in <code>swagger\/v1\/<\/code><\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In <code>routes.rb<\/code>, we define the routes for the application including the API routes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Couchbase connection and collection references are configured in the <code>config\/initializers\/couchbase.rb<\/code> file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this application, we have created the connection object in <code>config\/initializers\/couchbase.rb<\/code> and we use this object in all of our APIs. The object is initialized in <code>config\/initializers\/couchbase.rb<\/code>. We have also stored the reference to our bucket, <code>travel-sample<\/code> and the scope, <code>inventory<\/code> in the connection object.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code>require &#039;couchbase&#039;\n\n# Get environment variables\nDB_USERNAME = ENV[&#039;DB_USERNAME&#039;]\nDB_PASSWORD = ENV[&#039;DB_PASSWORD&#039;]\nDB_CONN_STR = ENV[&#039;DB_CONN_STR&#039;]\nDB_BUCKET_NAME = &#039;travel-sample&#039; # Hardcoded bucket name\n\n# Check if running in CI environment\nif ENV[&#039;CI&#039;]\n  # Use environment variables from GitHub Secrets\n  options = Couchbase::Cluster::ClusterOptions.new\n  options.authenticate(DB_USERNAME, DB_PASSWORD)\n  COUCHBASE_CLUSTER = Couchbase::Cluster.connect(DB_CONN_STR, options)\nelse\n  # Load environment variables from dev.env file\n  require &#039;dotenv&#039;\n  Dotenv.load(&#039;dev.env&#039;)\n\n  # Define default values\n  DEFAULT_DB_USERNAME = &#039;Administrator&#039;\n  DEFAULT_DB_PASSWORD = &#039;password&#039;\n  DEFAULT_DB_CONN_STR = &#039;couchbase:\/\/localhost&#039;\n\n  # Get environment variables with fallback to default values\n  DB_USERNAME = ENV.fetch(&#039;DB_USERNAME&#039;, DEFAULT_DB_USERNAME)\n  DB_PASSWORD = ENV.fetch(&#039;DB_PASSWORD&#039;, DEFAULT_DB_PASSWORD)\n  DB_CONN_STR = ENV.fetch(&#039;DB_CONN_STR&#039;, DEFAULT_DB_CONN_STR)\n\n  # Connect to the Couchbase cluster\n  options = Couchbase::Cluster::ClusterOptions.new\n  options.authenticate(DB_USERNAME, DB_PASSWORD)\n  COUCHBASE_CLUSTER = Couchbase::Cluster.connect(DB_CONN_STR, options)\nend\n\n# Open the bucket\nbucket = COUCHBASE_CLUSTER.bucket(DB_BUCKET_NAME)\n\n# Open the default collection\ndefault_collection = bucket.default_collection\n\n# Create scope and collections if they don&#039;t exist\nbegin\n  scope = bucket.scope(&#039;inventory&#039;)\nrescue Couchbase::Error::ScopeNotFoundError\n  bucket.create_scope(&#039;inventory&#039;)\n  scope = bucket.scope(&#039;inventory&#039;)\nend\n\n%w[airline airport route].each do |collection_name|\n  scope.collection(collection_name)\nrescue Couchbase::Error::CollectionNotFoundError\n  scope.create_collection(collection_name)\nend\n\nAIRLINE_COLLECTION = scope.collection(&#039;airline&#039;)\nAIRPORT_COLLECTION = scope.collection(&#039;airport&#039;)\nROUTE_COLLECTION = scope.collection(&#039;route&#039;)\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above code, we first get the environment variables for the connection string, username, and password. We then create a connection object using the <code>Couchbase::Cluster.connect<\/code> method. We authenticate the connection using the username and password. We then open the bucket and the default collection. We create a scope and collections if they don&#8217;t exist. We store the reference to the collections in the connection object.<\/p>\n\n\n\n<h4 id=\"h-airline-entity\" class=\"wp-block-heading\">Airline Entity<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">For this tutorial, we will focus on the airline entity to demonstrate CRUD operations and SQL++ queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We will be setting up a REST API to manage airline documents.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#post-airline\">POST Airline<\/a> \u2013 Create a new airline<\/li>\n\n\n<li><a href=\"#get-airline\">GET Airline<\/a> \u2013 Read specified airline<\/li>\n\n\n<li><a href=\"#put-airline\">PUT Airline<\/a> \u2013 Update specified airline<\/li>\n\n\n<li><a href=\"#delete-airline\">DELETE Airline<\/a> \u2013 Delete airline<\/li>\n\n\n<li><a href=\"#list-airline\">Airline List<\/a> \u2013 Get all airlines. Optionally filter the list by country<\/li>\n\n\n<li><a href=\"#direct-connections\">Direct Connections<\/a> &#8211; Get a list of airlines directly connected to the specified airport<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For CRUD operations, we will use the <a href=\"https:\/\/docs.couchbase.com\/ruby-sdk\/current\/howtos\/kv-operations.html\">Key-Value operations<\/a> that are built into the Couchbase SDK to create, read, update, and delete a document. Every document will need an ID (similar to a primary key in other databases) to save it to the database. This ID is passed in the URL. For other end points, we will use <a href=\"https:\/\/docs.couchbase.com\/ruby-sdk\/current\/howtos\/n1ql-queries-with-sdk.html\">SQL++<\/a> to query for documents.<\/p>\n\n\n\n<h4 id=\"h-airline-document-structure\" class=\"wp-block-heading\">Airline Document Structure<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Our airline document will have an airline name, IATA code, ICAO code, callsign, and country. For this demo, we will store all airline information in one document in the <code>airline<\/code> collection in the <code>travel-sample<\/code> bucket.<\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{\n  &quot;name&quot;: &quot;40-Mile Air&quot;,\n  &quot;iata&quot;: &quot;Q5&quot;,\n  &quot;icao&quot;: &quot;MLA&quot;,\n  &quot;callsign&quot;: &quot;MILE-AIR&quot;,\n  &quot;country&quot;: &quot;United States&quot;\n}\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-post-airline\" class=\"wp-block-heading\">POST Airline<\/h4>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code># frozen_string_literal: true\n\nmodule Api\n  module V1\n    class AirlinesController &lt; ApplicationController\n      skip_before_action :verify_authenticity_token, only: %i[create update destroy]\n      before_action :set_airline, only: %i[show update destroy]\n\n      ...\n      # POST \/api\/v1\/airlines\/{id}\n      def create\n        @airline = Airline.create(params[:id], airline_params)\n        if @airline\n          render json: @airline, status: :created\n        else\n          render json: { message: &#039;Airline already exists&#039; }, status: :conflict\n        end\n      rescue ArgumentError =&gt; e\n        render json: { error: &#039;Invalid request&#039;, message: e.message }, status: :bad_request\n      rescue Couchbase::Error::DocumentExists =&gt; e\n        render json: { error: &quot;Airline with ID #{params[:id]} already exists&quot;, message: e.message }, status: :conflict\n      rescue StandardError =&gt; e\n        render json: { error: &#039;Internal server error&#039;, message: e.message }, status: :internal_server_error\n      end\n    end\n    ... \n\n  end\nend\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above code, we first create an instance of the <code>Airline<\/code> class using the <code>create<\/code> method. We pass the ID and the airline data to the <code>create<\/code> method. If the airline is created successfully, we return the airline document with a status of 201. If the airline already exists, we return a conflict status of 409. If there are any missing fields in the request, we return a bad request status of 400. If there is any other error, we return an internal server error status of 500.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code># frozen_string_literal: true\n\nclass Airline\n  attr_accessor :name, :iata, :icao, :callsign, :country\n\n  def initialize(attributes)\n    @name = attributes[&#039;name&#039;]\n    @iata = attributes[&#039;iata&#039;]\n    @icao = attributes[&#039;icao&#039;]\n    @callsign = attributes[&#039;callsign&#039;]\n    @country = attributes[&#039;country&#039;]\n  end\n  ...\n\n  def self.create(id, attributes)\n    required_fields = %w[name iata icao callsign country]\n    missing_fields = required_fields - attributes.keys\n    extra_fields = attributes.keys - required_fields\n\n    raise ArgumentError, &quot;Missing fields: #{missing_fields.join(&#039;, &#039;)}&quot; if missing_fields.any?\n\n    raise ArgumentError, &quot;Extra fields: #{extra_fields.join(&#039;, &#039;)}&quot; if extra_fields.any?\n\n    formatted_attributes = {\n      &#039;name&#039; =&gt; attributes[&#039;name&#039;],\n      &#039;iata&#039; =&gt; attributes[&#039;iata&#039;],\n      &#039;icao&#039; =&gt; attributes[&#039;icao&#039;],\n      &#039;callsign&#039; =&gt; attributes[&#039;callsign&#039;],\n      &#039;country&#039; =&gt; attributes[&#039;country&#039;]\n    }\n    AIRLINE_COLLECTION.insert(id, formatted_attributes)\n    new(formatted_attributes)\n  end\n\n  ... \nend\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above code, we first check if all the required fields are present in the request. If any fields are missing, we raise an ArgumentError with the missing fields. If there are any extra fields in the request, we raise an ArgumentError with the extra fields. We then format the attributes and insert the document into the database using the <code>insert<\/code> method. We then create a new instance of the <code>Airline<\/code> class with the formatted attributes and return it.<\/p>\n\n\n\n<h4 id=\"h-get-airline\" class=\"wp-block-heading\">GET Airline<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Navigate to the <code>show<\/code> method in the <code>AirlinesController<\/code> class in the <code>airlines_controller.rb<\/code> file. We only need the airline document ID or our key from the user to retrieve a particular airline document using a key-value operation which is passed in the <code>find<\/code> method. The result is converted into a JSON object using the <code>to_json<\/code> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code>  # GET \/api\/v1\/airlines\/{id}\n  def show\n    if @airline\n      render json: @airline, status: :ok\n    else\n      render json: { error: &quot;Airline with ID #{params[:id]} not found&quot; }, status: :not_found\n    end\n  rescue Couchbase::Error::DocumentNotFound =&gt; e\n    render json: { error: &quot;Airline with ID #{params[:id]} not found&quot;, message: e.message }, status: :not_found\n  rescue StandardError =&gt; e\n    render json: { error: &#039;Internal server error&#039;, message: e.message }, status: :internal_server_error\n  end\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>find<\/code> method in the <code>Airline<\/code> class is used to fetch the document from the database using the key. If the document is found, we return the document with a status of 200. If the document is not found, we return a not found status of 404. If there is any other error, we return an internal server error status of 500.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code>  def self.find(id)\n    result = AIRLINE_COLLECTION.get(id)\n    new(result.content) if result.success?\n  rescue Couchbase::Error::DocumentNotFound\n    nil\n  end\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the <code>find<\/code> method, we use the <code>get<\/code> method to fetch the document from the database using the key. If the document is found, we create a new instance of the <code>Airline<\/code> class with the document content and return it. If the document is not found, we return <code>nil<\/code>.<\/p>\n\n\n\n<h4 id=\"h-put-airline\" class=\"wp-block-heading\">PUT Airline<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Navigate to the <code>update<\/code> method in the <code>AirlinesController<\/code> class in the <code>airlines_controller.rb<\/code> file. We only need the airline document ID or our key from the user to update a particular airline document using a key-value operation which is passed in the <code>update<\/code> method. The result is converted into a JSON object using the <code>to_json<\/code> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code>    # PUT \/api\/v1\/airlines\/{id}\n    def update\n      @airline = Airline.new(airline_params).update(params[:id], airline_params)\n      render json: @airline, status: :ok\n    rescue ArgumentError =&gt; e\n      render json: { error: &#039;Invalid request&#039;, message: e.message }, status: :bad_request\n    rescue StandardError =&gt; e\n      render json: { error: &#039;Internal server error&#039;, message: e.message }, status: :internal_server_error\n    end\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Inn the above code, we first create an instance of the <code>Airline<\/code> class using the <code>update<\/code> method. We pass the ID and the airline data to the <code>update<\/code> method. If the airline is updated successfully, we return the airline document with a status of 200. If there are any missing fields in the request, we return a bad request status of 400. If there is any other error, we return an internal server error status of 500.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code>  def update(id, attributes)\n    required_fields = %w[name iata icao callsign country]\n    missing_fields = required_fields - attributes.keys\n    extra_fields = attributes.keys - required_fields\n\n    raise ArgumentError, &quot;Missing fields: #{missing_fields.join(&#039;, &#039;)}&quot; if missing_fields.any?\n\n    raise ArgumentError, &quot;Extra fields: #{extra_fields.join(&#039;, &#039;)}&quot; if extra_fields.any?\n\n    formatted_attributes = {\n      &#039;name&#039; =&gt; attributes[&#039;name&#039;],\n      &#039;iata&#039; =&gt; attributes[&#039;iata&#039;],\n      &#039;icao&#039; =&gt; attributes[&#039;icao&#039;],\n      &#039;callsign&#039; =&gt; attributes[&#039;callsign&#039;],\n      &#039;country&#039; =&gt; attributes[&#039;country&#039;]\n    }\n    AIRLINE_COLLECTION.upsert(id, formatted_attributes)\n    self.class.new(formatted_attributes)\n  end\n\n  def destroy(id)\n    AIRLINE_COLLECTION.remove(id)\n  end\nend\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the <code>update<\/code> method, we first check if all the required fields are present in the request. If any fields are missing, we raise an ArgumentError with the missing fields. If there are any extra fields in the request, we raise an ArgumentError with the extra fields. We then format the attributes and update the document in the database using the <code>upsert<\/code> method. We then create a new instance of the <code>Airline<\/code> class with the formatted attributes and return it.<\/p>\n\n\n\n<h4 id=\"h-delete-airline\" class=\"wp-block-heading\">DELETE Airline<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Navigate to the <code>destroy<\/code> method in the <code>AirlinesController<\/code> class in the <code>airlines_controller.rb<\/code> file. We only need the airline document ID or our key from the user to delete a particular airline document using a key-value operation which is passed in the <code>destroy<\/code> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code>    # DELETE \/api\/v1\/airlines\/{id}\n      def destroy\n        if @airline\n          if @airline.destroy(params[:id])\n            render json: { message: &#039;Airline deleted successfully&#039; }, status: :accepted\n          else\n            render json: { message: &#039;Failed to delete airline&#039; }, status: :bad_request\n          end\n        else\n          render json: { error: &quot;Airline with ID #{params[:id]} not found&quot; }, status: :not_found\n        end\n      rescue Couchbase::Error::DocumentNotFound =&gt; e\n        render json: { error: &quot;Airline with ID #{params[:id]} not found&quot;, message: e.message }, status: :not_found\n      rescue StandardError =&gt; e\n        render json: { error: &#039;Internal server error&#039;, message: e.message }, status: :internal_server_error\n      end\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above code, we first check if the airline document exists. If the document exists, we call the <code>destroy<\/code> method on the <code>Airline<\/code> class. If the document is deleted successfully, we return a status of 202. If the document is not deleted successfully, we return a bad request status of 400. If the document is not found, we return a not found status of 404. If there is any other error, we return an internal server error status of 500.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code>  def destroy(id)\n    AIRLINE_COLLECTION.remove(id)\n  end\nend\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-list-airlines\" class=\"wp-block-heading\">List Airlines<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Navigate to the <code>index<\/code> method in the <code>AirlinesController<\/code> class in the <code>airlines_controller.rb<\/code> file. We only need the country, limit, and offset values from the user to retrieve a list of airline documents using a SQL++ query. The result is converted into a JSON object using the <code>to_json<\/code> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code>  # GET \/api\/v1\/airlines\/list\n  def index\n    country = params[:country]\n    limit = params[:limit] || 10\n    offset = params[:offset] || 0\n\n    @airlines = Airline.all(country, limit, offset)\n\n    if @airlines.empty?\n      render json: { message: &#039;No airlines found&#039; }, status: :ok\n    else\n      render json: @airlines, status: :ok\n    end\n  rescue StandardError =&gt; e\n    render json: { error: &#039;Internal server error&#039;, message: e.message }, status: :internal_server_error\n  end\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above code, we first get the values for the country, limit, and offset from the request parameters. We then call the <code>all<\/code> method in the <code>Airline<\/code> class to fetch the list of airlines. If the list is empty, we return a status of 200 with a message. If the list is not empty, we return a status of 200 with the list of airlines. If there is any other error, we return an internal server error status of 500.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code>  def self.all(country = nil, limit = 10, offset = 0)\n    bucket_name = &#039;travel-sample&#039;\n    scope_name = &#039;inventory&#039;\n    collection_name = &#039;airline&#039;\n\n    query = country ? &quot;SELECT * FROM `#{bucket_name}`.`#{scope_name}`.`#{collection_name}` WHERE country = $country LIMIT $limit OFFSET $offset&quot; : &quot;SELECT * FROM `#{bucket_name}`.`#{scope_name}`.`#{collection_name}` LIMIT $limit OFFSET $offset&quot;\n    options = Couchbase::Cluster::QueryOptions.new\n    options.named_parameters(country ? { &#039;country&#039; =&gt; country, &#039;limit&#039; =&gt; limit.to_i,\n                                         &#039;offset&#039; =&gt; offset.to_i } : { &#039;limit&#039; =&gt; limit.to_i, &#039;offset&#039; =&gt; offset.to_i })\n\n    result = COUCHBASE_CLUSTER.query(query, options)\n    result.rows.map { |row| new(row.fetch(&#039;airline&#039;, {})) }.to_a\n  end\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the <code>all<\/code> method, we first define the bucket, scope, and collection names. We then build the SQL++ query based on the country parameter. We then create a <code>QueryOptions<\/code> object and set the named parameters based on the country parameter. We then execute the query using the <code>query<\/code> method and iterate over the results to create a list of <code>Airline<\/code> objects and return it.<\/p>\n\n\n\n<h4 id=\"h-direct-connections\" class=\"wp-block-heading\">Direct Connections<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Navigate to the <code>direct_connections<\/code> method in the <code>AirportController<\/code> class in the <code>airports_controller.rb<\/code> file. We only need the airport document ID or our key from the user to retrieve a list of airlines directly connected to the specified airport using a SQL++ query. The result is converted into a JSON object using the <code>to_json<\/code> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-rb\"><code>def direct_connections\n  destination_airport_code = params[:destinationAirportCode]\n  limit = params[:limit] || 10\n  offset = params[:offset] || 0\n\n  if destination_airport_code.blank?\n    render json: { message: &#039;Destination airport code is required&#039; }, status: :bad_request\n  else\n    @connections = Airport.direct_connections(destination_airport_code, limit, offset)\n    render json: @connections, status: :ok\n  end\nrescue StandardError =&gt; e\n  render json: { error: &#039;Internal server error&#039;, message: e.message }, status: :internal_server_error\nend\n<\/code><\/pre>\n\n\n\n<h3 id=\"h-test-structure\" class=\"wp-block-heading\">Test Structure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The application uses two types of tests with clear separation of concerns:<\/p>\n\n\n\n<h4 id=\"h-integration-tests-codespecrequestsapiv1code\" class=\"wp-block-heading\">Integration Tests (<code>spec\/requests\/api\/v1\/<\/code>)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Integration tests verify the actual functionality of API endpoints with real database operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Purpose:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Verify actual API functionality and business logic<\/li>\n\n\n<li>Test full CRUD operations (Create, Read, Update, Delete)<\/li>\n\n\n<li>Validate error handling and edge cases<\/li>\n\n\n<li>Ensure proper HTTP status codes and response formats<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Characteristics:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use real Couchbase travel-sample data<\/li>\n\n\n<li>Create and delete test documents<\/li>\n\n\n<li>Include comprehensive assertions<\/li>\n\n\n<li>Clean up created data in ensure blocks<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example integration test structure:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code language-ruby\"><code>describe &#039;POST \/api\/v1\/airlines\/{id}&#039; do\n  let(:airline_id) { &#039;airline_post&#039; }\n  let(:airline_params) do\n    {\n      &#039;name&#039; =&gt; &#039;40-Mile Air&#039;,\n      &#039;iata&#039; =&gt; &#039;Q5&#039;,\n      &#039;icao&#039; =&gt; &#039;MLA&#039;,\n      &#039;callsign&#039; =&gt; &#039;MILE-AIR&#039;,\n      &#039;country&#039; =&gt; &#039;United States&#039;\n    }\n  end\n\n  it &#039;creates an airline&#039; do\n    post &quot;\/api\/v1\/airlines\/#{airline_id}&quot;, params: { airline: airline_params }\n\n    expect(response).to have_http_status(:created)\n    expect(response.content_type).to eq(&#039;application\/json; charset=utf-8&#039;)\n    expect(JSON.parse(response.body)).to include(airline_params)\n  ensure\n    delete &quot;\/api\/v1\/airlines\/#{airline_id}&quot;  # Cleanup\n  end\nend\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Run command:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>bundle exec rspec spec\/requests\/api\/v1\/\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-swagger-documentation-tests-codespecrequestsswaggercode\" class=\"wp-block-heading\">Swagger Documentation Tests (<code>spec\/requests\/swagger\/<\/code>)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Swagger documentation tests generate OpenAPI\/Swagger documentation without performing actual database operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Purpose:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Generate OpenAPI 3.0 specification<\/li>\n\n\n<li>Document API contracts and schemas<\/li>\n\n\n<li>Provide interactive API documentation<\/li>\n\n\n<li>Define request\/response examples<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Characteristics:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Documentation-only (no database mutations)<\/li>\n\n\n<li>Use existing travel-sample data IDs for GET operations<\/li>\n\n\n<li>Use placeholder IDs for other operations<\/li>\n\n\n<li>Fast execution without setup\/teardown<\/li>\n\n\n<li>Reference integration tests in comments<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example swagger spec structure:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code language-ruby\"><code>path &#039;\/api\/v1\/airlines\/{id}&#039; do\n  get &#039;Retrieves an airline by ID&#039; do\n    tags &#039;Airlines&#039;\n    produces &#039;application\/json&#039;\n    parameter name: :id, in: :path, type: :string\n\n    response &#039;200&#039;, &#039;airline found&#039; do\n      let(:id) { &#039;airline_10&#039; }\n\n      run_test! do |response|\n        # Documentation-only - actual testing in spec\/requests\/api\/v1\/airlines_spec.rb\n      end\n    end\n\n    response &#039;404&#039;, &#039;airline not found&#039; do\n      let(:id) { &#039;invalid_id&#039; }\n\n      run_test! do |response|\n        # Documentation-only - actual testing in spec\/requests\/api\/v1\/airlines_spec.rb\n      end\n    end\n  end\nend\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Run command:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>bundle exec rake rswag:specs:swaggerize\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-benefits-of-test-separation\" class=\"wp-block-heading\">Benefits of Test Separation<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This two-tier approach provides several advantages:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Fast Documentation Generation:<\/strong> Swagger specs run quickly without database setup\/teardown<\/li>\n\n\n<li><strong>Comprehensive Testing:<\/strong> Integration tests thoroughly validate functionality<\/li>\n\n\n<li><strong>Clear Separation:<\/strong> Documentation concerns are separate from functional testing<\/li>\n\n\n<li><strong>Maintainability:<\/strong> Changes to API contracts don&#8217;t require modifying integration tests<\/li>\n\n\n<li><strong>Safety:<\/strong> Documentation generation doesn&#8217;t risk modifying production data<\/li>\n\n<\/ol>\n\n\n\n<h3 id=\"h-running-tests\" class=\"wp-block-heading\">Running Tests<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Integration tests are implemented using <a href=\"https:\/\/rspec.info\/\">RSpec<\/a> for all API endpoints. Tests verify operations by calling the API and confirming results in the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To run the integration tests:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>bundle exec rspec spec\/requests\/api\/v1\/\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To generate the Swagger\/OpenAPI documentation:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>bundle exec rake rswag:specs:swaggerize\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> The integration tests verify actual API functionality, while the swagger specs are documentation-only and generate the OpenAPI specification file.<\/p>\n\n\n\n<h3 id=\"h-appendix\" class=\"wp-block-heading\">Appendix<\/h3>\n\n\n\n<h4 id=\"h-extending-api-by-adding-new-entity\" class=\"wp-block-heading\">Extending API by Adding New Entity<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you would like to add another entity to the APIs, these are the steps to follow:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create the new entity (collection) in the Couchbase bucket. You can create the collection using the <a href=\"https:\/\/docs.couchbase.com\/sdk-api\/couchbase-ruby-client\/Couchbase\/Collection.html#create_collection-instance_method\">SDK<\/a> or via the <a href=\"https:\/\/docs.couchbase.com\/server\/current\/manage\/manage-settings\/manage-collections.html\">Couchbase Server interface<\/a>.<\/li>\n\n\n<li>Define the routes in a new file in the <code>api<\/code> folder similar to the existing routes like <code>airlines_controller.rb<\/code>.<\/li>\n\n\n<li>Add the new routes to the application in <code>routes.rb<\/code>.<\/li>\n\n\n<li>Add integration tests in a new file in the <code>spec\/requests\/api\/v1\/<\/code> folder (e.g., <code>spec\/requests\/api\/v1\/customers_spec.rb<\/code>) with comprehensive tests for all CRUD operations.<\/li>\n\n\n<li>Add Swagger documentation in the <code>spec\/requests\/swagger\/<\/code> folder (e.g., <code>spec\/requests\/swagger\/customers_spec.rb<\/code>) as documentation-only specs:\n<ul>\n<li>Use <code>run_test! do |response|<\/code> blocks with comments referencing integration tests<\/li>\n<li>Use existing travel-sample data IDs for GET operations<\/li>\n<li>Use placeholder IDs for create\/update\/delete operations<\/li>\n<\/ul>\n<\/li>\n\n\n<li>Run the command <code>bundle exec rake rswag:specs:swaggerize<\/code> to generate the Swagger documentation.<\/li>\n\n\n<li>The new entity is now ready to be used in the API.<\/li>\n\n<\/ul>\n\n\n\n<h4 id=\"h-running-self-managed-couchbase-cluster\" class=\"wp-block-heading\">Running Self Managed Couchbase Cluster<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you are running this quickstart with a self managed Couchbase cluster, you need to <a href=\"https:\/\/docs.couchbase.com\/server\/current\/manage\/manage-settings\/install-sample-buckets.html\">load<\/a> the travel-sample data bucket in your cluster and generate the credentials for the bucket.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Follow <a href=\"\/tutorial-couchbase-installation-options\">Couchbase Installation Options<\/a> for installing the latest Couchbase Database Server Instance.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You need to update the connection string and the credentials in the <code>dev.env<\/code> file in the source folder.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Note: Couchbase Server must be installed and running prior to running the Ruby on Rails App<\/p>\n<\/blockquote>\n\n\n\n<h4 id=\"h-swagger-documentation\" class=\"wp-block-heading\">Swagger Documentation<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Swagger documentation provides a clear view of the API including endpoints, HTTP methods, request parameters, and response objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Click on an individual endpoint to expand it and see detailed information. This includes the endpoint&#8217;s description, possible response status codes, and the request parameters it accepts.<\/p>\n\n\n\n<h5 id=\"h-trying-out-the-api\" class=\"wp-block-heading\">Trying Out the API<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">You can try out an API by clicking on the &#8220;Try it out&#8221; button next to the endpoints.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\n<p>Parameters: If an endpoint requires parameters, Swagger UI provides input boxes for you to fill in. This could include path parameters, query strings, headers, or the body of a POST\/PUT request.<\/p>\n<\/li>\n\n\n<li>\n<p>Execution: Once you&#8217;ve inputted all the necessary parameters, you can click the &#8220;Execute&#8221; button to make a live API call. Swagger UI will send the request to the API and display the response directly in the documentation. This includes the response code, response headers, and response body.<\/p>\n<\/li>\n\n<\/ul>\n\n\n\n<h5 id=\"h-models\" class=\"wp-block-heading\">Models<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Swagger documents the structure of request and response bodies using models. These models define the expected data structure using JSON schema and are extremely helpful in understanding what data to send and expect.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to connect to a Couchbase Capella cluster to create, read, update, and delete documents and how to write simple parametrized SQL++ queries. Prerequisites To run this prebuilt project, you will need: Couchbase Capella Configuration When running Couchbase using Capella, the following prerequisites need to be met. App Setup [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false,"inline_featured_image":false,"_acf":"","footnotes":""},"tutorial-languages":[579],"tutorial-tags":[581,546],"tutorial-technologies":[572,548,549],"tutorial-types":[550],"class_list":["post-10249","tutorials","type-tutorials","status-publish","hentry","tutorial-languages-ruby","tutorial-tags-rails","tutorial-tags-rest-api","tutorial-technologies-index","tutorial-technologies-kv","tutorial-technologies-query","tutorial-types-quickstart"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.8 (Yoast SEO v28.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Quickstart in Couchbase with Ruby on Rails - Couchbase<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/de\/developers\/tutorials\/quickstart-ruby-on-rails\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Quickstart in Couchbase with Ruby on Rails\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to connect to a Couchbase Capella cluster to create, read, update, and delete documents and how to write simple parametrized SQL++ queries. Prerequisites To run this prebuilt project, you will need: Couchbase Capella Configuration When running Couchbase using Capella, the following prerequisites need to be met. App Setup [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/de\/developers\/tutorials\/quickstart-ruby-on-rails\/\" \/>\n<meta property=\"og:site_name\" content=\"Couchbase\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Couchbase\/\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/10\/Cloud-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2548\" \/>\n\t<meta property=\"og:image:height\" content=\"1171\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@couchbase\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"21\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/developers\\\/tutorials\\\/quickstart-ruby-on-rails\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/developers\\\/tutorials\\\/quickstart-ruby-on-rails\\\/\",\"name\":\"Quickstart in Couchbase with Ruby on Rails - Couchbase\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/#website\"},\"datePublished\":\"2025-12-23T04:01:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/developers\\\/tutorials\\\/quickstart-ruby-on-rails\\\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.couchbase.com\\\/de\\\/developers\\\/tutorials\\\/quickstart-ruby-on-rails\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/developers\\\/tutorials\\\/quickstart-ruby-on-rails\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.couchbase.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Quickstart in Couchbase with Ruby on Rails\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/#website\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/\",\"name\":\"Couchbase\",\"description\":\"The Operational Data Platform for AI\u2122\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/#organization\"},\"alternateName\":\"Couchbase, Inc.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"de\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/#organization\",\"name\":\"Couchbase\",\"alternateName\":\"Couchbase, Inc.\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"contentUrl\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"width\":1129,\"height\":1129,\"caption\":\"Couchbase\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/de\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Couchbase\\\/\",\"https:\\\/\\\/x.com\\\/couchbase\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/CouchbaseServer\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/couchbase\\\/\",\"https:\\\/\\\/github.com\\\/couchbase\\\/\",\"https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Couchbase,_Inc.\"],\"description\":\"Couchbase is a database software company headquartered in San Jose, California, providing an operational data platform for AI used by hundreds of global enterprises.\",\"legalName\":\"Couchbase, Inc.\",\"foundingDate\":\"2011-01-01\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Quickstart in Couchbase with Ruby on Rails - Couchbase","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/de\/developers\/tutorials\/quickstart-ruby-on-rails\/","og_locale":"de_DE","og_type":"article","og_title":"Quickstart in Couchbase with Ruby on Rails","og_description":"In this tutorial, you will learn how to connect to a Couchbase Capella cluster to create, read, update, and delete documents and how to write simple parametrized SQL++ queries. Prerequisites To run this prebuilt project, you will need: Couchbase Capella Configuration When running Couchbase using Capella, the following prerequisites need to be met. App Setup [&hellip;]","og_url":"https:\/\/www.couchbase.com\/de\/developers\/tutorials\/quickstart-ruby-on-rails\/","og_site_name":"Couchbase","article_publisher":"https:\/\/www.facebook.com\/Couchbase\/","og_image":[{"width":2548,"height":1171,"url":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/10\/Cloud-2.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@couchbase","twitter_misc":{"Est. reading time":"21\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/de\/developers\/tutorials\/quickstart-ruby-on-rails\/","url":"https:\/\/www.couchbase.com\/de\/developers\/tutorials\/quickstart-ruby-on-rails\/","name":"Quickstart in Couchbase with Ruby on Rails - Couchbase","isPartOf":{"@id":"https:\/\/www.couchbase.com\/de\/#website"},"datePublished":"2025-12-23T04:01:49+00:00","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/de\/developers\/tutorials\/quickstart-ruby-on-rails\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/de\/developers\/tutorials\/quickstart-ruby-on-rails\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/de\/developers\/tutorials\/quickstart-ruby-on-rails\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/"},{"@type":"ListItem","position":2,"name":"Quickstart in Couchbase with Ruby on Rails"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/de\/#website","url":"https:\/\/www.couchbase.com\/de\/","name":"Couchbase","description":"The Operational Data Platform for AI\u2122","publisher":{"@id":"https:\/\/www.couchbase.com\/de\/#organization"},"alternateName":"Couchbase, Inc.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/de\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"de"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/de\/#organization","name":"Couchbase","alternateName":"Couchbase, Inc.","url":"https:\/\/www.couchbase.com\/de\/","logo":{"@type":"ImageObject","inLanguage":"de","@id":"https:\/\/www.couchbase.com\/de\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","contentUrl":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","width":1129,"height":1129,"caption":"Couchbase"},"image":{"@id":"https:\/\/www.couchbase.com\/de\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Couchbase\/","https:\/\/x.com\/couchbase","https:\/\/www.youtube.com\/c\/CouchbaseServer","https:\/\/www.linkedin.com\/company\/couchbase\/","https:\/\/github.com\/couchbase\/","https:\/\/en.wikipedia.org\/wiki\/Couchbase,_Inc."],"description":"Couchbase ist ein in San Jose, Kalifornien, ans\u00e4ssiges Unternehmen f\u00fcr Datenbanksoftware, das eine operative Datenplattform f\u00fcr KI bereitstellt, die von Hunderten von globalen Unternehmen eingesetzt wird.","legalName":"Couchbase, Inc.","foundingDate":"2011-01-01"}]}},"acf":[],"permalink_manager":null,"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/de\/wp-json\/wp\/v2\/tutorials\/10249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/de\/wp-json\/wp\/v2\/tutorials"}],"about":[{"href":"https:\/\/www.couchbase.com\/de\/wp-json\/wp\/v2\/types\/tutorials"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/de\/wp-json\/wp\/v2\/tutorials\/10249\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/de\/wp-json\/wp\/v2\/media?parent=10249"}],"wp:term":[{"taxonomy":"tutorial-languages","embeddable":true,"href":"https:\/\/www.couchbase.com\/de\/wp-json\/wp\/v2\/tutorial-languages?post=10249"},{"taxonomy":"tutorial-tags","embeddable":true,"href":"https:\/\/www.couchbase.com\/de\/wp-json\/wp\/v2\/tutorial-tags?post=10249"},{"taxonomy":"tutorial-technologies","embeddable":true,"href":"https:\/\/www.couchbase.com\/de\/wp-json\/wp\/v2\/tutorial-technologies?post=10249"},{"taxonomy":"tutorial-types","embeddable":true,"href":"https:\/\/www.couchbase.com\/de\/wp-json\/wp\/v2\/tutorial-types?post=10249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}