Chapter 6 Flashcards
default data structure for a data model
model M in MVC.
Model
rails solution to use a database for long-term data storage
active record
default library for interacting with the database
benefit of active record
comes with host of methods for creating, saving, and finding data objects without having to use SQL used by relational databases.
migration
allow data definitions to be written in pure ruby, without having to learn SQL language. way to alter the structure of the database incrementally, so our data model can adapt to changing requirements
goals of a model
create model for users that wont disappear after creating them
why dont we need to identify attributes explicitly when modelling users.
rails uses relational database by default, which composed of data rows, where each row has columns of data attributes. active record will figure out the User object attributes fro us from the database
why we dont need to include id as parameter for user
it is created by rails for use as the primary key in the database
command for making a model with name and email attributes
rails generate model User name:string email:string
model name are singular
true
db/migrate/[timestamp]_create_user.rb
timestamp based on when the migration was generated
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps null: false end
change method that determines the change to be made to the database. rails method called create_table to create a table in the database for storing users. create_table accepts a block with one block variable called “t”.inside block, uses the t object to create name and email columns.
create_table :users do |t|. why is users plural
even though the model is singular, rails follows a linguistic convention. a database table consists of many users.
t.timestamps null:false
creates two magic columns called created_at and updated_at, which are timestamps that automatically record when a given user is created and updated.
bundle exec rake db:migrate
updates the database with our new users data model. creates a file called db/development.sqlite3, which is an sqlite database.