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.
how to see development.sqlite3
with DB Browser for SQLite. download it, then download development.sqlite3 and open to view it
migrate down and undo a migration
bundle exec rake db:rollback
class User
User inherits from ActiveRecord::Base so that the User model automatically has all the functionality of the ActiveRecord::Base class.
opening up console in a sandbox
rails console –sandbox. any modifications you make will be rolled back on exit.
User.new for models
do not have to ‘require’ for models. rails console automatically loads the rails environment which includes the model.
create new user with name and email
user = User.new(name: “name”, email: “email”)
user = User.new….
user.valid?
boolean valid? returns if object is valid
save object to database
user.save. returns true if it succeeds
user.updated_at
get time user was updated
create new user in database with one line and store in variable
foo = User.create(name: “name”, email: “email”)
destroy object
foo.destroy. object still exists in memory if you run foo. we need to learn about Active Record to fix this
User.find(1)
find user with id 1. ActiveRecord method for finding objects
User.find(2) (when we deleated 2)
raise exception saying couldnt find user with id = 2
find user with email
User.find_by(email: “email”)
ActiveRecord method for finding user with specific attributes.
get first user in database
User.first (A.R method)
return all users
User.all (A.R method) returns all the users in the database as an object of class ActiveRecord::Relation which is effectively an array
update user object email
user. email = “newEmail”
user. save
user.email
“email”
user.email = “newEmail”
user.reload.email gives what?
“email”. without saving, reloads object based in the database information
update user with activerecord method
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
command to test just the model tests
bundle exec rake test:models
test for validity for user object in user_test.rb
test “should be valid” do
assert @user.valid?
end