This goes a little into how to run SQL. You don’t need to know everything, but this is a great tutorial on straight SQL. Good news though, you don’t have to touch any SQL for this. Here’s the tl;dr:
- A SQL table is a lot like a spreadsheet. You have rows and columns. You should have a table for each data model.
- Tables and their columns need to manually created and removed. When you need to make a new column, you need to manually tell the system that it needs to be created.
- “Primary” column(s) are unique and define it. Generally speaking this is just an id number. Rails sets it up so that the database will auto increment the id so you don’t have to worry.
So let’s setup our database. We’ll be using Postgres, so we need to install it. For Macs, use Postgres.app. It’s simple and easy. Postgres provides an installer for Windows. And Linux can use your favorite package manager.
And once it’s setup, we need a database (set of tables) to put it into. You can use the included GUI or the command “createdb” to create a database.
Then we need to install the pg gem, which might be a small issue on any machine. For example on Mac you may need to run env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
to give it the right config file to build the C extension against. Googling eventually finds the right answer.
Hopefully now you have the gem installed, and we can run bundler with pg and ActiveRecord:
1 2 3 4 5 6 7 8 |
ruby '2.1.0' source 'https://rubygems.org' gem 'rack', '~>1.5.2' gem 'actionpack', '~> 4.1.0.beta1' gem 'actionview', '~> 4.1.0.beta1' gem 'railties', '~> 4.1.0.beta1' gem 'activerecord', '~> 4.1.0.beta1' gem 'pg' |
And the Railtie:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
require File.expand_path('../boot', __FILE__) require 'rails' require 'action_controller/railtie' require 'action_view/railtie' require 'active_record/railtie' Bundler.require module GenericCMS class Application < Rails::Application # you could put some custom stuff here if you wanted config.secret_key_base = "rails really wants this to be defined" end end |
Now we need to setup the postgres password. Create a new file, config/database.yml, to put the postgres password that you used to setup the postgres root password:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem 'sqlite3' development: adapter: postgresql encoding: utf8 reconnect: false database: NAMEOFDATABASE pool: 5 username: USERNAME password: PASSWORD host: localhost |
Don’t forget to change the pertinent parts.
With this we can create our first migration to get the database to have the right table information. So let’s create that file:
1 2 3 4 5 6 7 8 9 10 11 |
class CreatePages < ActiveRecord::Migration def change create_table :pages do |t| t.string :name t.string :handle t.text :body_text t.timestamps end end end |
So that’s a mouthful that’s easier explained in the Rails guide. In short this creates a table with the listed columns. By default the id primary column is create for you, as well as an automatic “created_at” and “updated_at” columns too.
To run our migration you need to run bundle exec rake db:migrate
. This occurs whenever you need to run any database migrations.
And now let’s update our Page model:
1 2 3 4 |
class Page < ActiveRecord::Base validates_presence_of :name validates_length_of :name, minimum: 3 end |
Yep, it is that small. Rails is smart enough to know that the attributes is the name of the columns. It also includes a finder method and handles saving to the database for you. Of course we need to create the “create page” page since we have an empty database:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
class PagesController < ApplicationController def new @page = Page.new render :action=>:edit end def create @page = Page.new @page.name = params['page']['name'] @page.handle = params['page']['handle'] @page.body_text = params['page']['body_text'] if @page.save redirect_to @page else render :action=>:edit end end def show @page = Page.find(params[:id]) end def edit @page = Page.find(params[:id]) end def update @page = Page.find(params[:id]) @page.name = params['page']['name'] @page.handle = params['page']['handle'] @page.body_text = params['page']['body_text'] if @page.save redirect_to @page else render :action=>:edit end end end |
Notice how we don’t even need to create a new form? It can re-use the same form because it knows that the data is a new entry instead of an old entry and redirects accordingly.
Lastly, there’s associations, which you can read on the Rails guide. We’ll be going into associations when we start the main project, but for now the guide will do.
And that gets you up to speed with all the major parts of Rails. There’s still tons more to learn, but there’s better guides for what you need after this point:
- How to query the database to get certain items or groupings
- How to send email
- All about the asset pipeline that takes care of CSS and Javascript files for you
And that’s it. This last part is available on Github.