orm-mapping-db-to-ruby-object-lab-online-web-sp-000

  • g5_285099
    了解作者
  • 9.3KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-26 09:05
    上传日期
ORM将数据库行映射到Ruby对象 目标 编写将数据库行转换为Ruby对象的方法 编写允许您搜索数据库的方法 概述 我们将继续建立我们应用程序的Student类。我们正在构建一个应用程序,以帮助公立学校的管理员跟踪学生。在这个程序中,我们有一个Student班。每个学生都有两个属性,即name和grade ,即9th,10th,11th等。 我们的管理员需要将此类生成的学生对象保存到数据库中。我们已经在config/environment.rb文件中为您设置了数据库。 让我们来看看: require 'sqlite3' require_relative '../lib/student' DB = { :conn => SQLite3 :: Database . new ( "db/students.db" ) } 您可以在整个程序中引用您与数据库的连接,如下所示: DB[:conn] 。
orm-mapping-db-to-ruby-object-lab-online-web-sp-000-master.zip
  • orm-mapping-db-to-ruby-object-lab-online-web-sp-000-master
  • .gitignore
    642B
  • .rspec
    53B
  • LICENSE.md
    1.3KB
  • CONTRIBUTING.md
    1.8KB
  • db
  • students.db
    0B
  • spec
  • student_spec.rb
    5.2KB
  • spec_helper.rb
    86B
  • README.md
    3.5KB
  • config
  • environment.rb
    109B
  • lib
  • student.rb
    893B
  • Gemfile
    132B
  • .learn
    71B
  • Gemfile.lock
    668B
内容介绍
# ORM Mapping Database Rows to Ruby Objects ## Objectives - Write a method that turns database rows into Ruby objects - Write methods that allow you to search a database ## Overview We are going to continue building out the `Student` class of our app. We are in the middle of building an app to help a public school's administrators keep track of their students. In this app, we have a `Student` class. Each individual student has two attributes, a `name` and a `grade`, i.e. 9th, 10th, 11th, etc. Our administrators need to save the student objects that this class produces to a database. We've set up the database for you in the `config/environment.rb` file. Let's take a look: ```ruby require 'sqlite3' require_relative '../lib/student' DB = {:conn => SQLite3::Database.new("db/students.db")} ``` Your connection to the database can be referred to, throughout your program, like this: `DB[:conn]`. So far we built the ability to create the `students` table in the database (`Student.create_table`), drop that table (`Student.drop_table`), and `#save` a student to the database. Now, we will need to create a method that takes a row from the database and turns it back into a Student object. We will call this `.new_from_db`. Next, we want to build a couple of methods to get information from the database. We will call these `.find_by_name` and `.all`. Let's get started! ## Instructions ### The `.new_from_db` Method This is a class method that accepts a row from the database as an argument. It then creates a new student object based on the information in the row. Remember, our database doesn't store Ruby objects, so we have to manually convert it ourselves. ### The `.find_by_name` Method This is a class method that accepts the name of a student. You will first run a SQL query to get the result from the database where the student's name matches the name passed into the argument. Next, you will take the result and create a new student instance using the `.new_from_db` method you just created. ### The `.all` Method This is a class method that is very similar to the `.find_by_name` method. You will not need an argument since we are returning everything in the database. Run the SQL to return everything (`*`) from a table. Again, you will use the `.new_from_db` method to create a student instance for each row that comes back from the database. ### The `.all_students_in_grade_9` Method This is a class method that does not need an argument. This method should return an array of all the students in grade 9. ### The `.students_below_12th_grade` Method This is a class method that does not need an argument. This method should return an array of all the students below 12th grade. ### The `.first_X_students_in_grade_10` Method This is a class method that takes in an argument of the number of students from grade 10 to select. This method should return an array of exactly `X` number of students. ### The `.first_student_in_grade_10` Method This is a class method that does not need an argument. This should return the first student that is in grade 10. ### The `.all_students_in_grade_X` Method This is a class method that takes in an argument of a grade for which to retrieve the roster. This method should return an array of all students for grade `X`. <p data-visibility='hidden'>View <a href='https://learn.co/lessons/orm-mapping-db-to-ruby-object-lab' title='ORM Mapping Database Rows to Ruby Objects' rel='nofollow' onclick='return false;'>ORM Mapping Database Rows to Ruby Objects</a> on Learn.co and start learning to code for free.</p>
评论
    相关推荐