Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```ruby
- ACTIVE RECORD LECTURE -KEVIN (092817)
- Active record playground
- ruby <-- active record --> SQL
- why ruby?
- - framework rails ( what we use to write a website )
- - automation
- why SQL? database
- - only allows you to store data; no automation
- ######
- #SQL command
- # -create : insert into
- # -read : SELECT
- # -update : UPDATE
- # -delete (data): DELETE FROM
- #which name do you connect to? based on the class name?
- # class name person, then table is people
- # rb(main):001:0> load 'playground.rb'
- # => true
- CREATE and SAVE NEW student/ object
- #this just creates objects but does not include to the database.
- # irb(main):004:0> a = Student.new
- # => #<Student id: nil, name: nil, email: nil, age: nil, cre
- # ated_at: nil, updated_at: nil>
- # rb(main):005:0> b = Student.new(name: 'kevin', email: 'ke
- # vin@example.com')
- # => #<Student id: nil, name: "kevin", email: "kevin@example
- # .com", age: nil, created_at: nil, updated_at: nil>
- # how to put this into the database?
- # rb(main):006:0> b.save
- # => true
- #tudent@728c41844461:~$ sqlite3 students.sqlite3
- #sqlite> select * from students;
- #kevin has been entered
- ############################################
- # irb
- # loairb(main):001:0> load 'playground.rb'
- When saved, then you have automatically assigned id (incremented id)
- # rb(main):002:0> boy = Student.new(name: 'boy', email: 'boy@boy
- # .com')
- => <Student id: nil, name: "boy", email: "boy@boy.com", age: n
- # il, created_at: nil, updated_at: nil>
- # irb(main):003:0> boy.save
- # => true
- # irb(main):004:0> boy
- => <Student id: 22, name: "boy", email: "boy@boy.com", age: ni
- # l, created_at: "2017-09-28 07:35:07", updated_at: "2017-09-28 0
- UPDATE changing attribute (update)
- boy.update(name: 'girl', email: "notboy@boy.co
- m)
- find 'kevin' first
- #assume that I know is 21 my id
- Student.find(21)
- # => #<Student id: 21, name: "kevin", email: "kevin@example.com",
- # age: nil, created_at: "2017-09-28 07:27:07", updated_at: "2017
- # -09-28 07:27:07">
- Kevin = Student.find(21)
- # #<Student id: 21, name: "kevin", email: "kevin@example.com",
- # age: nil, created_at: "2017-09-28 07:27:07", updated_at: "2017
- # -09-28 07:27:07">
- Student.find_by(email: 'kevin@example.com')
- # => #<Student id: 21, name: "kevin", email: "kevin@example.com",
- # age: nil, created_at: "2017-09-28 07:27:07", updated_at: "2017
- # -09-28 07:27:07">
- # ActiveRecord::Base library?
- SAVE
- # a.name = "kevinsia"
- # => "kevinsia"
- # irb(main):017:0> a.save
- # => true
- Student.all & Last Student
- # a = Student.all
- #a[-1] => last student
- # last_student = Student.last
- #last_student.name
- Update
- # y = Student.find(12)
- # y.update(email: 'haha@hah.com')
- WHERE: find same name more than one (i.e. kevinsia)
- # Student.where(name: 'kevinsia')
- # => #<ActiveRecord::Relation [#<Student id: 21, name: "kevinsia"
- # , email: "kevin@hansome.com", age: nil, created_at: "2017-09-28
- # 07:27:07", updated_at: "2017-09-28 07:45:55">]>
- .count : count the number
- # Student.where(name: 'kevinsia').count
- # => 2
- age less than?
- Student.where('age <?', 18)
- #Student.where('age <?', 18).count => 5
- name starts with K?
- Student.where('name LIKE ?', 'k%')
- # => #<ActiveRecord::Relation [#<Student id: 21, name: "kevinsia"
- # , email: "kevin@hansome.com", age: nil, created_at: "2017-09-28
- # 07:27:07", updated_at: "2017-09-28 07:45:55">, #<Student id: 2
- # 4, name: "kevinsia", email: "different@g.com", age: nil, create
- # d_at: "2017-09-28 08:00:21", updated_at: "2017-09-28 08:00:21">
- # ]>
- #BE CAREFUL! SQL injection = avoid name LIKE k%
- WHERE & Two conditions
- Student.where('name LIKE ? AND age <?', 'M%',18)
- # => #<ActiveRecord::Relation [#<Student id: 11, name: "Miss Toy
- # Hickle", email: "tracy.kunde@herman.info", age: 17, created_at:
- # "2017-01-25 08:44:17", updated_at: "2017-01-25 08:44:17">]>
- # irb(main):027:0>
- a.destroy or Student.destry(24)
- #a.dstory
- # => #<Student id: 21, name: "kevinsia", email: "kevin@hansome.co
- # m", age: nil, created_at: "2017-09-28 07:27:07", updated_at: "2
- # 017-09-28 07:45:55">
- # Student.destroy(24)
- # => #<Student id: 24, name: "kevinsia", email: "different@g.com"
- # , age: nil, created_at: "2017-09-28 08:00:21", updated_at: "201
- # 7-09-28 08:00:21">
- behaves like array! :example1
- Student.where("age <?", 18).each do |student|
- puts student.name
- end
- #=> Sofia Gleichner IV Rod Harvey Miss Toy Hickle Hardy Jerde Beau Sanford
- behaves like array! :example2
- Student.where("age <?", 18).each do |student|
- puts "#{student.id}:#{student.name},#{student.
- email}"end
- # 7:Sofia Gleichner IV,evalyn@feeney.org
- # 8:Rod Harvey,janea.lindgren@flatleybrown.com
- # 11:Miss Toy Hickle,tracy.kunde@herman.info
- # 13:Hardy Jerde,raquel_schamberger@hudsondickinson.com
- # 15:Beau Sanford,thelma_altenwerth@swaniawski.info
- ####################################
- # Write your test code here
- # 1. Create a new student using new and save
- b = Student.new
- b.save
- # 2. Create a new student using create
- Student.create(name: 'kevin')
- # 3. Select all students
- Student.all
- # pay attention to the object that is returned to you, is it a Student object or Active Record relation object?
- #Relations object?
- Active Record relation object is a collection of student object; generate from class.all, class. where
- actual object you can create, update, save.
- # Why can't you do Student.name or Student.email?
- you cant put .(dot) on Class to get name or email. you can do it on one individual student object
- # pay attention to the object that is returned to you, is it a Student object or Active Record relation object?
- #Student.all returned <ActiveRecord::Relation [
- # 4. Select the first student
- => #<Student id: 1, name: "Grayson Bednar", email: "emmy@schneider.net", age:
- #20, created_at: "2017-01-25 08:44:05", updated_at: "2017-01-25 08:44:05">
- # 5. Select the last student
- #<Student id: 20, name: "Miss Lori Johns", email: "maximo@batz.net", age:
- #20, created_at: "2017-01-25 08:44:21", updated_at: "2017-01-25 08:44:21">
- # pay attention to the object that is returned to you, is it a Student object or Active Record relation object?
- #STUDENT object
- # 6. Use each to iterate through the #<ActiveRecord::Relation> object to display the name of each student
- Student.where("age<?",18) each do |student| puts student.name end
- # 7. Find student by the name Dr. Lois Pfeffer using where.
- # pay attention to the object that is returned to you, is it the Student object or Active Record relation object?
- Student.where('name LIKE ?', "Dr%")
- # What will the following return to you? (remember to comment out the code!)
- # p student.id
- # p student.name
- # p student.email
- # p student.age
- # 8. Find student by name using find_by
- # pay attention to the object that is returned to you, is it the Student object or Active Record relation object?
- # What will the following return to you?
- # p student.id
- # p student.name
- # p student.email
- # p student.age
- => there is nothing stored in student. you create new object and then
- # Do research on the difference between "where" and "find_by".
- # 9. Find the student with id = 7 using find and find_by
- # pay attention to the object that is returned to you, is it the User object or Active Record relation object?
- Student.find(7)
- Student.find_by(id: 7)
- # 10. Update information for student with id = 5 using student.email and save. Change the student's email to elsie@example.com
- student = Student.find(5)
- student.update(email: "elase@example.com")
- # 11. Now use update to update this student's age to 21.
- # 12. Delete student with id 21 using destroy
- # 13. Delete student with id 22 using destroy
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement