A Ruby on rails project containing a music database (an Album model and a Track model). The data comes from the db>seeds.rb
file.
Made during The Hacking Project course on Week5 Day3.
Project is created with
- Ruby on Rails 5.2.3
First, go to the RoR project folder and type:
bundle install
then
rails c
Copy/paste the corresponding commands for each question in the console to display the answer.
-
What is the total number of Album objects contained in the database ?
Album.count
-
Who is the author of the song "White Room"?
Track.find_by(title: "White Room").artist
-
Which song lasts exactly 188133 milliseconds?
Track.find_by(duration: 188133).title
-
Which band released the album "Use Your Illusion II" ?
Album.find_by(title: "Use Your Illusion II").artist
-
How many albums have "Great" in their title?
Album.where("title like ?", "%great%").count
-
eletes all the albums whose name contains "music".
Album.where("title like ?", "%music%").destroy_all
-
How many albums are there written by AC/DC ?
Album.where(artist: "AC/DC").count
-
How many songs last exactly 158589 milliseconds?
Track.where(duration: 158589).count
-
puts in console all the titles of AC/DC.
Track.where(artist: "AC/DC").each do |track| puts track.title end
-
puts in console all the titles of the album "Let There Be Rock".
Track.where(album: "Let There Be Rock").each do |track| puts track.title end
-
Calculate the total price of this album and its total duration.
price = Track.where(album: "Let There Be Rock").sum(:price).round(2) duration = Track.where(album: "Let There Be Rock").sum(:duration) puts "#{price}, #{duration}"
-
Calculate the cost of the entire "Deep Purple" discography.
Track.where(artist: "Deep Purple").sum(:price).round(2)
-
Modify (via a loop) all the titles of "Eric Clapton" so that they are displayed with "Britney Spears" in artist.
Track.where(artist: "Eric Clapton").each do |track| track.update(artist: 'Britney Spears') end