Just looking for the final code?  It's here.

Models

Open up the labs/2-models.rb file.  We'll be inserting rows into the salespeople table.

It might be helpful to check out the schema file to recall the structure of the salespeople table: it has columns for first_name, last_name and email (along with the usual id, created_at, and updated_at columns).

Insert rows into the salespeople table.

Now let's insert a couple rows into the table:

salesperson = Salesperson.new
salesperson["first_name"] = "Benjamin"
salesperson["last_name"] = "Block"
salesperson["email"] = "ben@test.com"
salesperson.save

salesperson = Salesperson.new
salesperson["first_name"] = "Brian"
salesperson["last_name"] = "Eng"
salesperson["email"] = "brian@test.com"
salesperson.save

To run this code, enter the following command in terminal:

rails runner labs/2-models.rb

Display number of rows in salespeople table.

You won't see any output when inserting the rows above, but you can check the log file or check the data in sqlite3.  Or, we can add some code to display how many rows exist before and after the insert code - it'll be a nice sanity check that our code is working and doesn't require checking anywhere else.  Let's add the following code before and after.

puts "Salespeople: #{Salesperson.all.count}"

Now when we run the file, we'll see the output:

Salespeople: 0
Salespeople: 2

Each time we run the file, we'll get the same output.  Why isn't the file adding 2 more rows each time it runs?  The file has a line of code at the top:

Salesperson.destroy_all

This deletes all the rows in the table before the rest of the code runs, effectively resetting the table data.

Modify a row in the salespeople table.

Now that we've added a couple rows to the table, let's practice modifying a row.  We'll first find a row - remember when querying for a single row, we need to use a query that will (or at least should) only return a single result.  Something unique like an id or email is ideal.  In this case, we'll use email:

ben = Salesperson.find_by({"email" => "ben@test.com"})

Now that we have the row, let's re-assign an attribute and then save the row.

ben["first_name"] = "Ben"
ben.save

If we check the data, we'll see that the first_name column for this row changed from "Benjamin" to "Ben".

Challenge!

Display each salesperson's full name.

It's common to want to display data from multiple rows (either all of the rows or some subset based on a query condition).  So let's practice displaying the full name of each salesperson.  We could find each row individually and then read the relevant columns, but that's inefficient and we may not know all the rows in advance (without looking at the database).  So, hopefully you're starting to guess that we need a loop.

Let's start by getting all the salespeople rows:

salespeople = Salesperson.all

This code returns an array of all the rows in the salespeople table.  Next, let's loop through the array with a loop variable:

for salesperson in salespeople
  # ...code to display a salesperson's name
end

This loop will iterate through the array of salespeople rows and, on each iteration, assign a row into the salesperson variable.  On the first iteration, this will be the first row; on the second iteration, the second row; etc.

Now, inside the loop, we just read the relevant columns first_name and last_name and then print them in a string:

for salesperson in salespeople
  # read the relevant columns from the salesperson row
  first_name = salesperson["first_name"]
  last_name = salesperson["last_name"]

  # display a string with the relevant columns
  puts "#{first_name} #{last_name}"
end

That's it.  The output from running this entire file will look something like this (depending on the data you inserted in step 1 of this lab):

Salespeople: 2
Ben Block
Brian Eng