- Create a table using ActiveRecord
- Use Active Record's querying methods.
In this lab, we'll be working in a TV show domain. We have a TV Show model, the
Show class, and a shows table that you'll create in your database.
You'll be required not only to write a migration that creates a shows table and gives the
Show class certain attributes, but also to use Active Record's querying methods.
Active Record Querying Methods
Active Record makes it easy to ask our database for certain information and datasets by providing a bunch of built-in methods for us to use to do just that. For example, we can request the sum of all of the values of a particular column with the
#sum method like this:
We can query our database based on certain conditions using the
Let's say we have a
Song class and table and each song has rating,
number_of_stars attribute. We could query for songs with more than 3 stars like this:
Song.where("number_of_starts > ?", 3)
Let's look at one more example: Let's say we wanted to query our database for the lowest value in the
number_of_stars column, i.e. the lowest rating that any song has:
You'll be writing methods that rely on Active Record methods like
#where to get these tests passing.
Use the following resources to help you find the Active Record query methods that will help you pass these tests:
- Create a file in the
001_create_shows.rb. In this file, write the migration code to create a shows table. The table should have name, network, day, network and rating columns. Name, network and day have a datatype of string and rating has a datatype of integer.
- Create a file,
app/models. In this file you will define a
Showclass that inherits from Active Record Base.
- Now we need to create a second migration to add another column to our shows table. In the
db/migratefolder, create another file,
002_add_season_to_shows.rband write a migration to add a column, season, to the shows table. The datatype of this column is string.
You'll be defining the following methods:
#highest_rating: this method should return the highest value in the ratings column. hint: if there is a
#minimumActive Record method, might there be a
#most_popular_show: this method should return the show with the highest rating. hint: use the
#highest_ratingmethod as a helper method.
#lowest_rating: returns the lowest value in the ratings column.
#least_popular_show: returns the show with the lowest rating.
#ratings_sum: returns the sum of all of the ratings.
#popular_shows: returns an array of all of the shows that have a rating greater than
5. hint: use the
#wheremethod Active Record method.
#shows_by_alphabetical_order: returns an array of all of the shows sorted by alphabetical order according to their names. hint: use the
#orderActive Record method. View this lesson on Learn.co