BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Active Records 2020

RubyOnRails_logo




Bookmark and Share





bogotobogo.com site search:





Active Records

Active Record is the M in MVC.

Active Records are used in Ruby as a means of persisting data so that it can be recalled for later use even after we've closed the program that created the data.

The active Record pattern is used to access data stored in relational databases, and it allows us to perform CRUD operations without worrying about the specific underying database technology (SQLite, MySQL, or PostgresSQL, etc).




Most software application today are written using OO language, and often it is necessary to persist the objects associated with these applications.

However, the classes and objects associated with an OO language are incompatible with the structure of relational databases. Active Records' object-relational mapping (ORM) does a mapping between OO language constructs and relational database constructs.

The ORM provided by Active Records automatically converts object into constructs that can be stored in a database and converts them back upon retrieval. This creates, in effect, a 'virtual objct database' that can be used from within an OO language.


OO_DB.png

From class.coursera.org/webapplications.



Using the functionality provided by ActiveRecord module, we can do the followings:

  1. Establish a connection to a database.
  2. Create database tables.
  3. Specify associations between tables that correspond to associations between the Ruby classes.
  4. Establish an ORM between Ruby classes/objects/attributes and the tables/rows/columns in the underlying database.
  5. Perform CRUD operations on Ruby ActiveRecord objects.

The ActiveRecord module is built into Rails so that the functionalities listed above are utilized when we create a Rails app and run scaffold and model generators.

The ActiveRecord::Base.establish_connection method uses the information in ./config/database.yml in order to connect a Rails application to a database:

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default;
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

The ActiveRecord::Migration object is used to incrementally evolve our database over time:

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.text :body

      t.timestamps
    end
  end
end

The ActiveRecord::Schema.define method, in ./db/schema:

ActiveRecord::Schema.define(version: 20141031050956) do

  create_table "comments", force: true do |t|
    t.integer  "post_id"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "posts", force: true do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end
is created by inspecting the database and then expressing its structure programmically using a portable (database-independent) DSL (Domain Specific Language). This can be loaded into any database that ActiveRecord supports.





Naming Conventions

By default, Active Record uses some naming conventions to find out how the mapping between models and database tables should be created.

Rails will pluralize our class names to find the respective database table. So, for a class Book, we should have a database table called books.

The Rails pluralization mechanisms are very powerful, being capable to pluralize (and singularize) both regular and irregular words.

When using class names composed of two or more words, the model class name should follow the Ruby conventions, using the CamelCase form, while the table name must contain the words separated by underscores.

Examples:

  1. Database Table - Plural with underscores separating words (e.g., book_clubs).
  2. Model Class - Singular with the first letter of each word capitalized (e.g., BookClub).

For more info, please check Active Record Basics.









Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Ruby on Rails



Ruby On Rails Home

Ruby - Input/Output, Objects, Load

Ruby - Condition (if), Operators (comparison/logical) & case statement

Ruby - loop, while, until, for, each, (..)

Ruby - Functions

Ruby - Exceptions (raise/rescue)

Ruby - Strings (single quote vs double quote, multiline string - EOM, concatenation, substring, include, index, strip, justification, chop, chomp, split)

Ruby - Class and Instance Variables

Ruby - Class and Instance Variables II

Ruby - Modules

Ruby - Iterator : each

Ruby - Symbols (:)

Ruby - Hashes (aka associative arrays, maps, or dictionaries)

Ruby - Arrays

Ruby - Enumerables

Ruby - Filess

Ruby - code blocks and yield

Rails - Embedded Ruby (ERb) and Rails html

Rails - Partial template

Rails - HTML Helpers (link_to, imag_tag, and form_for)

Layouts and Rendering I - yield, content_for, content_for?

Layouts and Rendering II - asset tag helpers, stylesheet_link_tag, javascript_include_tag

Rails Project

Rails - Hello World

Rails - MVC and ActionController

Rails - Parameters (hash, array, JSON, routing, and strong parameter)

Filters and controller actions - before_action, skip_before_action

The simplest app - Rails default page on a Shared Host

Redmine Install on a Shared Host

Git and BitBucket

Deploying Rails 4 to Heroku

Scaffold: A quickest way of building a blog with posts and comments

Databases and migration

Active Record

Microblog 1

Microblog 2

Microblog 3 (Users resource)

Microblog 4 (Microposts resource I)

Microblog 5 (Microposts resource II)

Simple_app I - rails html pages

Simple_app II - TDD (Home/Help page)

Simple_app III - TDD (About page)

Simple_app IV - TDD (Dynamic Pages)

Simple_app V - TDD (Dynamic Pages - Embedded Ruby)

Simple_app VI - TDD (Dynamic Pages - Embedded Ruby, Layouts)

App : Facebook and Twitter Authentication using Omniauth oauth2

Authentication and sending confirmation email using Devise

Adding custom fields to Devise User model and Customization

Devise Customization 2. views/users

Rails Heroku Deploy - Authentication and sending confirmation email using Devise

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger I

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger II

OOPS! Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger (Trouble shooting)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master