8/17/2017 - 6:57 PM

Rails Stuff

Rake, migration, pry with pow, adding AR validations to plain Ruby objects

Rake Tasks:

  • Used to allow us to automate certain jobs (executing SQL for example)
  • Create a file in the top level directory of a rails project called Rakefile
  • Write your tasks in lib/tasks/name.rake (if one doesn't already exist)
  • to define a task:
task :hello do
  # code you want to execute with this task
  • You define a task with the syntax: task :name_of_task + a do/end block
task :hello do
  puts "Hello from rake"
  • In your terminal you can execute this task by running:
rake hello

Being more specific about our tasks:

  • You can run rake -T to see view a list BUT in order for this to work, we need to give our tasks descriptions
desc 'Outputs hello to the terminal'
task :hello do
  puts "hello from rake"

Namespacing routes (grouping tasks under one common title)

namspace :greeting do
  desc 'Outputs hello to the terminal'
  task :hello do
    puts "Hello from the terminal"
  desc 'Ouputs goodbye to the terminal'
  task :goodbye do
    puts "Goodbye from the terminal"

  • To access the above tasks, run:
rake greeting:hello
rake greeting:goodbye

Database related jobs

namespace :db do
  desc 'migrates changes to your database'
  task :migrate => :environment do
  • task :migrate => :environment
    • The above line creates a task dependency. The Student.create_table would require access to the config/environment.rb file (which is where the student class and database are loaded)
  • you would need to give access to this task
  • run this task before the migration:
task :environment do
  require_relative './config/environment'

Connecting a Postgres DB to a Rails app:

gem install pg
  • create role myapp with createdb login password 'password1';
  • This creates a user (or role as PSQL calls it)
  • To create a Rails app configured for Postgres, run this command:
rails new myapp --database=postgres
  • You can configure which database Rails will talk to, this is done using the database.yml file, which is located at config/database.yml

  • the database.yml file is used by Rails to connect to the appropriate database

  • YAML = data serialization standard

  • Rails convention for the username is the name of the app

  • the password should match the password of the user from step 2

  • Next add the following to each environment (ex development) in the database.yml file:

  adapter: postgresql
  encoding: unicode
  database: nameofapp_development
  • Start psql server
  • Run rake db:setup
  • Visit the page to see it everything is working
  • Might have to run rake db:migrate
  • (You can check in pgadmin if the databases were created)
  • These lines of code are used to set up a default settings that you can then load in to each environment area (development, testing, production).
  • You would have to manually type these settings in each area
  • To save code, use the following syntax:
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: parms
  timeout: 5000 
  • Then in each area of the environment, to load these settings in to them:
  <<: *default

Resource Generators/Routes


  • Migrations
  • Models
  • Controllers
rails generate <name of generator> name_of_generator <options> --no-test-framework
-- OR
rails g <name of generator> nameOfGenerator<options> --no-test-framework

rails destroy <name of generator> name #undo rails g
-- OR
rails d <name of generator> name

-- To generate a resource within a namespace:
rails g resource namespace_name/name_of_resource

  • This will generate the model, controller, route and migration file!
rails g resource name fields --no-test-framework

To create a new table in Rails

  • Create the model and it will also create the table for you
rails g model name_of_model table_column_name:type
rails g model ServiceLocations address:string

To add column

rails g migration AddColumnNameToTable col1:type col2:type ...

To remove a column

rails g migration RemoveColumnNameFromTable colName

Rolling back a migration:

rake db:rollback

Adding fields to a migration:

t.datatype :name

Using pry with pow

  • in gemfile: gem 'pry-remote', :group => :development
  • Install with bundle bundle install
  • insert this statement in your code binding.remote_pry
  • in your terminal, run: pry-remote

Adding AR Validations to Plain Ruby Objects

  • add this to your ruby class: include ActiveModel::Validations

Specifying ruby gemset and ruby version

  • create 2 files in your apps root directory: .ruby-gemset and .ruby-version
  • place the name of the gemset and version # of the ruby in their respective files