Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. ```ruby
  2. ACTIVE RECORD LECTURE -KEVIN (092817)
  3.  
  4. Active record playground
  5.  
  6. ruby <-- active record --> SQL
  7.  
  8. why ruby?
  9. - framework rails ( what we use to write a website )
  10. - automation
  11. why SQL? database
  12. - only allows you to store data; no automation
  13.  
  14.  
  15. ######
  16. #SQL command
  17. # -create : insert into
  18. # -read : SELECT
  19. # -update : UPDATE
  20. # -delete (data): DELETE FROM
  21.  
  22.  
  23. #which name do you connect to? based on the class name?
  24.  
  25. # class name person, then table is people
  26.  
  27.  
  28.  
  29. # rb(main):001:0> load 'playground.rb'
  30. # => true
  31.  
  32.  
  33. CREATE and SAVE NEW student/ object
  34.  
  35. #this just creates objects but does not include to the database.
  36.  
  37. # irb(main):004:0> a = Student.new
  38. # => #<Student id: nil, name: nil, email: nil, age: nil, cre
  39. # ated_at: nil, updated_at: nil>
  40.  
  41.  
  42.  
  43. # rb(main):005:0> b = Student.new(name: 'kevin', email: 'ke
  44. # vin@example.com')
  45. # => #<Student id: nil, name: "kevin", email: "kevin@example
  46. # .com", age: nil, created_at: nil, updated_at: nil>
  47.  
  48.  
  49. # how to put this into the database?
  50. # rb(main):006:0> b.save
  51. # => true
  52.  
  53.  
  54. #tudent@728c41844461:~$ sqlite3 students.sqlite3
  55. #sqlite> select * from students;
  56. #kevin has been entered
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. ############################################
  64.  
  65. # irb
  66. # loairb(main):001:0> load 'playground.rb'
  67.  
  68.  
  69. When saved, then you have automatically assigned id (incremented id)
  70.  
  71. # rb(main):002:0> boy = Student.new(name: 'boy', email: 'boy@boy
  72. # .com')
  73. => <Student id: nil, name: "boy", email: "boy@boy.com", age: n
  74. # il, created_at: nil, updated_at: nil>
  75. # irb(main):003:0> boy.save
  76. # => true
  77. # irb(main):004:0> boy
  78. => <Student id: 22, name: "boy", email: "boy@boy.com", age: ni
  79. # l, created_at: "2017-09-28 07:35:07", updated_at: "2017-09-28 0
  80.  
  81.  
  82. UPDATE changing attribute (update)
  83. boy.update(name: 'girl', email: "notboy@boy.co
  84. m)
  85.  
  86.  
  87.  
  88. find 'kevin' first
  89. #assume that I know is 21 my id
  90.  
  91. Student.find(21)
  92. # => #<Student id: 21, name: "kevin", email: "kevin@example.com",
  93. # age: nil, created_at: "2017-09-28 07:27:07", updated_at: "2017
  94. # -09-28 07:27:07">
  95.  
  96. Kevin = Student.find(21)
  97. # #<Student id: 21, name: "kevin", email: "kevin@example.com",
  98. # age: nil, created_at: "2017-09-28 07:27:07", updated_at: "2017
  99. # -09-28 07:27:07">
  100.  
  101.  
  102. Student.find_by(email: 'kevin@example.com')
  103. # => #<Student id: 21, name: "kevin", email: "kevin@example.com",
  104. # age: nil, created_at: "2017-09-28 07:27:07", updated_at: "2017
  105. # -09-28 07:27:07">
  106.  
  107.  
  108.  
  109. # ActiveRecord::Base library?
  110.  
  111. SAVE
  112. # a.name = "kevinsia"
  113. # => "kevinsia"
  114. # irb(main):017:0> a.save
  115. # => true
  116.  
  117.  
  118. Student.all & Last Student
  119. # a = Student.all
  120. #a[-1] => last student
  121. # last_student = Student.last
  122.  
  123. #last_student.name
  124.  
  125.  
  126. Update
  127. # y = Student.find(12)
  128. # y.update(email: 'haha@hah.com')
  129.  
  130.  
  131.  
  132. WHERE: find same name more than one (i.e. kevinsia)
  133.  
  134. # Student.where(name: 'kevinsia')
  135. # => #<ActiveRecord::Relation [#<Student id: 21, name: "kevinsia"
  136. # , email: "kevin@hansome.com", age: nil, created_at: "2017-09-28
  137. # 07:27:07", updated_at: "2017-09-28 07:45:55">]>
  138.  
  139.  
  140. .count : count the number
  141. # Student.where(name: 'kevinsia').count
  142. # => 2
  143.  
  144.  
  145. age less than?
  146. Student.where('age <?', 18)
  147. #Student.where('age <?', 18).count => 5
  148.  
  149.  
  150. name starts with K?
  151. Student.where('name LIKE ?', 'k%')
  152. # => #<ActiveRecord::Relation [#<Student id: 21, name: "kevinsia"
  153. # , email: "kevin@hansome.com", age: nil, created_at: "2017-09-28
  154. # 07:27:07", updated_at: "2017-09-28 07:45:55">, #<Student id: 2
  155. # 4, name: "kevinsia", email: "different@g.com", age: nil, create
  156. # d_at: "2017-09-28 08:00:21", updated_at: "2017-09-28 08:00:21">
  157. # ]>
  158.  
  159. #BE CAREFUL! SQL injection = avoid name LIKE k%
  160.  
  161.  
  162. WHERE & Two conditions
  163. Student.where('name LIKE ? AND age <?', 'M%',18)
  164. # => #<ActiveRecord::Relation [#<Student id: 11, name: "Miss Toy
  165. # Hickle", email: "tracy.kunde@herman.info", age: 17, created_at:
  166. # "2017-01-25 08:44:17", updated_at: "2017-01-25 08:44:17">]>
  167. # irb(main):027:0>
  168.  
  169.  
  170.  
  171. a.destroy or Student.destry(24)
  172. #a.dstory
  173. # => #<Student id: 21, name: "kevinsia", email: "kevin@hansome.co
  174. # m", age: nil, created_at: "2017-09-28 07:27:07", updated_at: "2
  175. # 017-09-28 07:45:55">
  176.  
  177. # Student.destroy(24)
  178. # => #<Student id: 24, name: "kevinsia", email: "different@g.com"
  179. # , age: nil, created_at: "2017-09-28 08:00:21", updated_at: "201
  180. # 7-09-28 08:00:21">
  181.  
  182.  
  183.  
  184. behaves like array! :example1
  185. Student.where("age <?", 18).each do |student|
  186. puts student.name
  187. end
  188.  
  189. #=> Sofia Gleichner IV Rod Harvey Miss Toy Hickle Hardy Jerde Beau Sanford
  190.  
  191.  
  192.  
  193. behaves like array! :example2
  194. Student.where("age <?", 18).each do |student|
  195. puts "#{student.id}:#{student.name},#{student.
  196. email}"end
  197.  
  198.  
  199. # 7:Sofia Gleichner IV,evalyn@feeney.org
  200. # 8:Rod Harvey,janea.lindgren@flatleybrown.com
  201. # 11:Miss Toy Hickle,tracy.kunde@herman.info
  202. # 13:Hardy Jerde,raquel_schamberger@hudsondickinson.com
  203. # 15:Beau Sanford,thelma_altenwerth@swaniawski.info
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212. ####################################
  213. # Write your test code here
  214.  
  215. # 1. Create a new student using new and save
  216. b = Student.new
  217. b.save
  218. # 2. Create a new student using create
  219. Student.create(name: 'kevin')
  220.  
  221. # 3. Select all students
  222. Student.all
  223. # pay attention to the object that is returned to you, is it a Student object or Active Record relation object?
  224. #Relations object?
  225. Active Record relation object is a collection of student object; generate from class.all, class. where
  226. actual object you can create, update, save.
  227.  
  228. # Why can't you do Student.name or Student.email?
  229. you cant put .(dot) on Class to get name or email. you can do it on one individual student object
  230.  
  231. # pay attention to the object that is returned to you, is it a Student object or Active Record relation object?
  232. #Student.all returned <ActiveRecord::Relation [
  233.  
  234.  
  235. # 4. Select the first student
  236. => #<Student id: 1, name: "Grayson Bednar", email: "emmy@schneider.net", age:
  237. #20, created_at: "2017-01-25 08:44:05", updated_at: "2017-01-25 08:44:05">
  238.  
  239.  
  240. # 5. Select the last student
  241. #<Student id: 20, name: "Miss Lori Johns", email: "maximo@batz.net", age:
  242. #20, created_at: "2017-01-25 08:44:21", updated_at: "2017-01-25 08:44:21">
  243. # pay attention to the object that is returned to you, is it a Student object or Active Record relation object?
  244. #STUDENT object
  245.  
  246.  
  247.  
  248. # 6. Use each to iterate through the #<ActiveRecord::Relation> object to display the name of each student
  249. Student.where("age<?",18) each do |student| puts student.name end
  250.  
  251.  
  252. # 7. Find student by the name Dr. Lois Pfeffer using where.
  253. # pay attention to the object that is returned to you, is it the Student object or Active Record relation object?
  254. Student.where('name LIKE ?', "Dr%")
  255. # What will the following return to you? (remember to comment out the code!)
  256. # p student.id
  257. # p student.name
  258. # p student.email
  259. # p student.age
  260.  
  261.  
  262. # 8. Find student by name using find_by
  263. # pay attention to the object that is returned to you, is it the Student object or Active Record relation object?
  264.  
  265. # What will the following return to you?
  266. # p student.id
  267. # p student.name
  268. # p student.email
  269. # p student.age
  270.  
  271. => there is nothing stored in student. you create new object and then
  272.  
  273. # Do research on the difference between "where" and "find_by".
  274.  
  275. # 9. Find the student with id = 7 using find and find_by
  276. # pay attention to the object that is returned to you, is it the User object or Active Record relation object?
  277. Student.find(7)
  278. Student.find_by(id: 7)
  279.  
  280.  
  281. # 10. Update information for student with id = 5 using student.email and save. Change the student's email to elsie@example.com
  282. student = Student.find(5)
  283. student.update(email: "elase@example.com")
  284. # 11. Now use update to update this student's age to 21.
  285.  
  286. # 12. Delete student with id 21 using destroy
  287.  
  288. # 13. Delete student with id 22 using destroy
  289.  
  290.  
  291.  
  292.  
  293. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement