Chapter 6 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

default data structure for a data model

A

model M in MVC.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Model

A

rails solution to use a database for long-term data storage

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

active record

A

default library for interacting with the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

benefit of active record

A

comes with host of methods for creating, saving, and finding data objects without having to use SQL used by relational databases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

migration

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

goals of a model

A

create model for users that wont disappear after creating them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

why dont we need to identify attributes explicitly when modelling users.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

why we dont need to include id as parameter for user

A

it is created by rails for use as the primary key in the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

command for making a model with name and email attributes

A

rails generate model User name:string email:string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

model name are singular

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

db/migrate/[timestamp]_create_user.rb

A

timestamp based on when the migration was generated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

def change
create_table :users do |t|
t.string :name
t.string :email

  t.timestamps null: false
end
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

create_table :users do |t|. why is users plural

A

even though the model is singular, rails follows a linguistic convention. a database table consists of many users.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

t.timestamps null:false

A

creates two magic columns called created_at and updated_at, which are timestamps that automatically record when a given user is created and updated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

bundle exec rake db:migrate

A

updates the database with our new users data model. creates a file called db/development.sqlite3, which is an sqlite database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to see development.sqlite3

A

with DB Browser for SQLite. download it, then download development.sqlite3 and open to view it

17
Q

migrate down and undo a migration

A

bundle exec rake db:rollback

18
Q

class User

A

User inherits from ActiveRecord::Base so that the User model automatically has all the functionality of the ActiveRecord::Base class.

19
Q

opening up console in a sandbox

A

rails console –sandbox. any modifications you make will be rolled back on exit.

20
Q

User.new for models

A

do not have to ‘require’ for models. rails console automatically loads the rails environment which includes the model.

21
Q

create new user with name and email

A

user = User.new(name: “name”, email: “email”)

22
Q

user = User.new….

user.valid?

A

boolean valid? returns if object is valid

23
Q

save object to database

A

user.save. returns true if it succeeds

24
Q

user.updated_at

A

get time user was updated

25
Q

create new user in database with one line and store in variable

A

foo = User.create(name: “name”, email: “email”)

26
Q

destroy object

A

foo.destroy. object still exists in memory if you run foo. we need to learn about Active Record to fix this

27
Q

User.find(1)

A

find user with id 1. ActiveRecord method for finding objects

28
Q

User.find(2) (when we deleated 2)

A

raise exception saying couldnt find user with id = 2

29
Q

find user with email

A

User.find_by(email: “email”)

ActiveRecord method for finding user with specific attributes.

30
Q

get first user in database

A

User.first (A.R method)

31
Q

return all users

A

User.all (A.R method) returns all the users in the database as an object of class ActiveRecord::Relation which is effectively an array

32
Q

update user object email

A

user. email = “newEmail”

user. save

33
Q

user.email
“email”
user.email = “newEmail”
user.reload.email gives what?

A

“email”. without saving, reloads object based in the database information

34
Q

update user with activerecord method

A

user.update_attributes(name: “newName”, email: “canAddMoreAttributes”) (automatically updates the database and changes the updated_at timestamp) also user.update_attribute(:name, “name”) updates one attribute. if one of the update_attributes fails, then it all fails and wont update anything

35
Q

command to test just the model tests

A

bundle exec rake test:models

36
Q

test for validity for user object in user_test.rb

A

test “should be valid” do
assert @user.valid?
end