Skip to content

Instantly share code, notes, and snippets.

@misho-kr
Last active December 13, 2015 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misho-kr/4f95004387c3dded6910 to your computer and use it in GitHub Desktop.
Save misho-kr/4f95004387c3dded6910 to your computer and use it in GitHub Desktop.
Summary of "Rails with Active Record and Action Pack" online course on Coursera.Org

Rails with Active Record and Action Pack

Introduction to Active Record

In this module, we will begin exploring the database-interaction portion of Rails. We will start off with migrations that enable you to create and modify the schema of the database. We will then move on to discussing the Active Record gem Rails uses, which enables you to create, retrieve, update, and delete the data from the database. Before looking at Active Record, we will talk about some advanced Ruby features of meta-programming that will help facilitate our Active Record journey.

Slides

  • Rails scaffolding -- rails g scaffold <model-name> <column-name> [column-type]
    • Code generator for entities
    • Creates simple prototype, including REST API
    • Generates migration, model, routes, controller with actions, views
  • Apply scaffolding to database -- rake db:migrate, revert with rake db::rollback
  • Rails uses SQLite, can be switched, see config/database.yml
  • Database console rails db
  • Migrations -- Ruby classes that extend ActiveRecord::Migration
    • Create and drop tables
    • Add, remove and rename columns
  • Uses database table schema_migrations
  • Dynamic dispatch, calling method dynamically, define_method(), method_missing(), ghost methods
  • Struct and OpenStruct
  • ActiveRecord is Rails' default ORM
    • has to know how to find the database, config/database
    • there is a table with plural name corresponding to ActiveRecord::Base subclass with a singular name
    • expects table to have a primary key named id
  • Rails console rails c
  • Model has its own generator that can create a migration
    • rails g model <model-name> <column-name>
  • CRUD operations
    • create, save
    • find, find_by, first, last, take, all, where, order, pluck, limit, offset
    • update, save, update_all
    • destroy, delete

Deep Dive into Active Record

In this module, we will continue exploring Active Record and look at ways to code advanced queries without exposing ourselves to risk from SQL injection (as well as what SQL injection actually is). We will then look at expressing relationships between entities in Active Record and validating the data being saved to the database.

Slides

  • See database -- rake --describe db_seed
  • Danger of SQL injection, avoid by using bind variables with either:
    • Array condition syntax, similar to PreparedStatement in Java, have to keep track of all "?" by position, variables may have to be repeated
    • Hash condition syntax, it seems more intuitive
  • One-to-One association
    • The belongs_to side is where the foreign key resides
    • Foreign key is "{ master_table_singular }_id"
    • The primay table class gets has_one:
    • Primary table class ets two extra methods:
      • build{ secondary_table_singular }_ to create new class instance
      • create{ secondary_table_singular }_ to create new class instance and save it in database right away
  • One-to-Many association
    • The belongs_to side is where the foreign key resides
    • Foreign key is "{ master_table_singular }_id"
    • The primay table class gets has_many:
    • More extra methods
      • Assigning and appending instances of secondary table class
      • Create and where methods become scoped to the primary table instance
      • dependent option to specify the fate of the association when the parent gets destroyed -- delete, destroy and nullify
  • Many-to-Many associations
    • Need to create extra join table, but there is no need for model class
    • Both classes get has_and_belongs_to_many:
    • Rich many-to-many associations
      • To store some data on the association table
      • through: option to support grandchild operations like user -> article -> comments
  • Scopes
    • default_scope: to specify how records are retrived from database, e.g. order by
    • Use unscoped to break out of the default scope
    • Named scopes -- scope :name, lambda, can be chained
  • Validators
    • Control over what goes into the database
    • Built-in and custom
  • Transactions
  • Avoding N+1 queries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment