ruby-on-railsrubygemsrailsapps

a simplification of what a rails project actually is


I'm at the point where I can make basic rails apps, and am looking for a better overview of how rails is working.

This is what I am visualising. Is this correct?

  1. Install the rails gem. This also installs a rails script.
  2. rails new ... creates the framework file structure.
  3. The rails script allows you to interact with your file structure from the command line, but isn't actually doing anything to the rails library itself.
  4. The "gem 'rails'" in the gem file imports the whole rails library into your new project so in a way, working on a rails app you really just extending the rails gem slightly (relatively speaking) to suit your needs

Solution

  • For an overview of Rails and how it works, you might find my article What is Ruby on Rails? helpful.

    Rails is a software library that extends the Ruby programming language. It is a package library (specifically, a RubyGem), that is installed using the operating system command-line interface. The gem contains several tools, including a generator (script) that creates a default Rails project (rails new ...). The default Rails project provides a set of files that conform to a set of conventions so all Rails applications have the same architecture.

    Rails is a framework for building websites. As such, Rails establishes conventions for easier collaboration and maintenance. These conventions are codified as the Rails API (the application programming interface, or directives that control the code). The Rails API is documented online and described in books, articles, and blog posts. Learning Rails means learning how to use the Rails conventions and its API.

    Many files created in the project directory by rails new ... inherit from parent classes that are hidden in the Rails gem, providing behavior that is defined in the Rails API. For example, when you create a model that inherits from ActiveRecord, you can use methods from the ActiveRecord API to access a database.

    When you run a Rails application with an application server, the custom code you've added to your Rails project directory is combined with the code provided in the Rails gem to respond to browser requests.

    Your simplified description isn't far off. There is no "rails script"; there are many generators (for creating a model, controller, project directory, etc.) that are used during development. The rails new ... generator (script) sets up a project directory from the command line but doesn't modify the Rails library provided by the gem. When you install the Rails gem in your Ruby development environment, the Rails software library (the API) is available to any Rails project you create in your file system. Any Rails project you build uses the code provided by the Rails gem, both to create initial project files and provide the full behavior of the framework. I'm not sure we'd say that a new project "extends" the Rails gem (that implies adding functionality that would be used in many projects) but certainly a new project incorporates or "builds on" the Rails gem.