🕵️♀️ Task
On an ActiveRecord::Relation, the pluck method will retrieve one or more attributes of each record in the collection and returns them in a Array. You can pass one or more Symbols as an argument. Each Symbol must match the name of a column.
where is used for filtering a collection of records down using various criteria. Most commonly, we pass a Hash argument. The key is the column you’re searching in and the value is what you’re trying to find in that column. The value could be any type of object, but the Class should usually match the type of column you’re searching in.
You can use an Array of as a value and include a list of values that you’d like to find in the column you’re searching in.
LevelUp
0.0% complete
LevelUp
0.0% complete
| id | title | year | duration | description | image | director_id |
|---|---|---|---|---|---|---|
| 17 | Seven Samurai | 1954 | 207 | A poor village under attack... | http://ia.media-imdb.com/im... | 14 |
| 18 | Forrest Gump | 1994 | 142 | Forrest Gump, while not int... | http://ia.media-imdb.com/im... | 15 |
| 19 | The Matrix | 1999 | 136 | A computer hacker learns fr... | http://ia.media-imdb.com/im... | 16 |
| 20 | The Lord of the Rings: The ... | 2002 | 179 | While Frodo and Sam edge cl... | http://ia.media-imdb.com/im... | 8 |
| 21 | City of God | 2002 | 130 | Two boys growing up in a vi... | http://ia.media-imdb.com/im... | 17 |
| 22 | Se7en | 1995 | 127 | Two detectives, a rookie an... | http://ia.media-imdb.com/im... | 9 |
| 23 | The Silence of the Lambs | 1991 | 118 | A young F.B.I. cadet must c... | http://ia.media-imdb.com/im... | 18 |
| 24 | Once Upon a Time in the West | 1968 | 175 | Epic story of a mysterious ... | http://ia.media-imdb.com/im... | 4 |
| id | name | dob | bio | image |
|---|---|---|---|---|
| 17 | Kátia Lund | http://ia.media-imdb.com/im... | ||
| 18 | Jonathan Demme | 1944-02-22 | http://ia.media-imdb.com/im... | |
| 19 | Michael Curtiz | 1886-12-24 | Curtiz began acting in and ... | http://ia.media-imdb.com/im... |
| 20 | Bryan Singer | 1965-09-17 | http://ia.media-imdb.com/im... | |
| 21 | Alfred Hitchcock | 1899-08-13 | Alfred Hitchcock was the so... | http://ia.media-imdb.com/im... |
| 22 | Frank Capra | 1897-05-18 | One of seven children, Fran... | http://ia.media-imdb.com/im... |
| 23 | Luc Besson | 1959-03-18 | Luc Besson spent the first ... | http://ia.media-imdb.com/im... |
| 24 | Billy Wilder | 1906-06-22 | Originally planning to beco... | http://ia.media-imdb.com/im... |
| id | name | dob | bio | image |
|---|---|---|---|---|
| 17 | Al Pacino | 1940-04-25 | Known for his forceful dram... | http://ia.media-imdb.com/im... |
| 18 | James Caan | 1940-03-26 | A masculine and enigmatic a... | http://ia.media-imdb.com/im... |
| 19 | Richard S. Castellano | 1933-09-04 | Bronx born, stocky Italian-... | http://ia.media-imdb.com/im... |
| 20 | Robert Duvall | 1931-01-05 | Veteran actor and director ... | http://ia.media-imdb.com/im... |
| 21 | Sterling Hayden | 1916-03-26 | Born to George & Frances Si... | http://ia.media-imdb.com/im... |
| 22 | John Marley | 1907-10-17 | Veteran character actor Joh... | http://ia.media-imdb.com/im... |
| 23 | Richard Conte | 1910-03-24 | Richard Conte was born Nich... | http://ia.media-imdb.com/im... |
| 24 | Al Lettieri | 1928-02-24 | Menacing looking Italian Am... | http://ia.media-imdb.com/im... |
| id | actor_id | movie_id | name |
|---|---|---|---|
| 17 | 17 | 2 | Michael Corleone |
| 18 | 18 | 2 | Sonny Corleone |
| 19 | 19 | 2 | Clemenza (as Richard Castel... |
| 20 | 20 | 2 | Tom Hagen |
| 21 | 21 | 2 | Capt. McCluskey |
| 22 | 22 | 2 | Jack Woltz |
| 23 | 23 | 2 | Barzini |
| 24 | 24 | 2 | Sollozzo |
Retrieve the ages of each record in the persons table
Person.all.pluck(:age)
=> [42, 36, 22, 29, 27]
Retrieve the names of each record in the dishes table
Dish.all.pluck(:name)
=> ["Burger", "Mac & Cheese", "Kale Salad"]
Select all records from the users table that have "joe" in the first_name column.
User.all.where({ :first_name => "joe" })
=> #<User::ActiveRecord_Relation:0x00007fa4341e1ee8>
Select all records from the dishes table that have 20 in the price column.
Dish.all.where({ :price => 20, :rating => 5 })
=> #<Dish::ActiveRecord_Relation:0x00007fa4341e1ee8>
Select the records from the cities table that have the values: 13, 25, and 63 in the id column.
City.all.where({ :id => [13, 25, 63]})
=> #<City::ActiveRecord_Relation:0x00007fa4341e1ee8>