Repeat the previous exercise but this time let's use the ActiveRecord query interface to find Carmen Sandiego.
You DO NOT need to create the database NOR load the data again.
Feel free to re-use the SQL solution of the previous exercise and translate the SQL queries into ActiveRecord queries.
Please use ActiveRecord queries to solve the following puzzles:
-
List the distinct regions in the
Country
table. -
How many countries are located in European regions?
-
Find the total population of all countries group by regions.
-
Use one query to answer the following 2 questions
-
Find the countries which have the most spoken languages used (Bonus: show the country names)
-
Find the maximum number of languages you can use in one country
-
Find all the Asia countries that went independent from 1940 to 1950. Order the result by the year of independence.
-
Find all the countries that do not use English at all (Bonus: show the country names)
- Fork and Clone
- In your local lab folder, do
cd code/carmen
. - You should NOT change anything in the
code/carmen
folder. - Run
rails c
and you will be inside the Rails console. Here you can use theActiveRecord
finder methods to query for data. - Try to run the query
City.take(1)
to ensure everything is in order. You should see a similar output like this one:
Running via Spring preloader in process 26598
Loading development environment (Rails 4.2.1)
irb(main):001:0> City.take(1)
City Load (0.4ms) SELECT "city".* FROM "city" LIMIT 1
=> [#<City id: 1, name: "Kabul", countrycode: "AFG", district: "Kabol", population: 1780000>]
irb(main):002:0>
- Collect all your
ActiveRecord
queries in a file calledquery.rb
under the repo root folder (where you find this README). - Submit a pull request