-
The rolodex application data should be managed by a PostgreSQL database in a Rails application.
- cd to Desktop
- rails new active-record -d postgresql -T
- cd to active-record
- run rails db:create
- run rails server
-
The model should be called Person with first_name, last_name, and phone attributes. All data types should be strings.
-
input in terminal under active-record a model class for Person: rails generate model Person first_name:string last_name:string phone:string
-
will output: invoke active_record create db/migrate/20231109183638_create_people.rb create app/models/person.rb
-
execute the migrate file in order to create our Person table in the database: rails db:migrate
-
at this point we have a table in the database for People
-
-
Add five friends and family members to the people table using the Rails console.
- Person.create(first_name: "Rhonda", last_name:"Nerat", phone: "999-111-8521")
- Executed the above 5 times with 5 different names and numbers
-
Retrieve all the people in the database.
- Person.all
- Printed all 5 instances of Person
-
Retrieve the third person in the database.
- Person.find(3)
-
Retrieve only the first name of the first person in the database.
- Person.first.first_name
-
Remove the last person from the database.
- Person.last.destroy
-
Add yourself to the people table.
- Person.create(first_name: "Kevin", last_name:"Rojas", phone: "999-111-3333")
-
Retrieve all the people that have the same last name as you
- Person.where(last_name:"Rojas")
-
Retrieve only the first person from the list of people that have the same last name as you.
- Person.where(last_name:"Rojas").first
-
Update the phone number of the second person in the database.
- Person.find(2).update(phone:"999-596-8888")
-
Retrieve the last name of the third person in the database.
- Person.find(3).last_name
____STRETCH
-
Update all the family members with the same last name as you to have the same phone number as you.
- created variable pnumber for Kevin's phone number
- Person.where(last_name:"Rojas").update(phone:pnumber)
-
Remove all family members that do not have your last name.
- Person.where.not(last_name:"Rojas").destroy_all