🕵️♀️ 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.
Returns a new relation, which is the result of filtering the current relation according to the conditions in the arguments.
whereaccepts conditions in one of several formats.
Given a
Hashargument, the keys are columns and the values are values to be searched for. Columns can be symbols or strings. Values can be single values, arrays, or ranges.
LevelUp
0.0% complete
LevelUp
0.0% complete
| id | title | year | duration | description | image | director_id |
|---|---|---|---|---|---|---|
| 1 | The Shawshank Redemption | 1994 | 142 | Two imprisoned men bond ove... | http://ia.media-imdb.com/im... | 1 |
| 2 | The Godfather | 1972 | 175 | The aging patriarch of an o... | http://ia.media-imdb.com/im... | 2 |
| 3 | The Godfather: Part II | 1974 | 200 | The early life and career o... | http://ia.media-imdb.com/im... | 2 |
| 4 | Pulp Fiction | 1994 | 154 | The lives of two mob hit me... | http://ia.media-imdb.com/im... | 3 |
| 5 | The Good, the Bad and the Ugly | 1966 | 161 | A bounty hunting scam joins... | http://ia.media-imdb.com/im... | 4 |
| 6 | The Dark Knight | 2008 | 152 | When Batman, Gordon and Har... | http://ia.media-imdb.com/im... | 5 |
| 7 | 12 Angry Men | 1957 | 96 | A dissenting juror in a mur... | http://ia.media-imdb.com/im... | 6 |
| 8 | Schindler's List | 1993 | 195 | In Poland during World War ... | http://ia.media-imdb.com/im... | 7 |
| id | name | dob | bio | image |
|---|---|---|---|---|
| 1 | Frank Darabont | 1959-01-28 | Three-time Oscar nominee Fr... | http://ia.media-imdb.com/im... |
| 2 | Francis Ford Coppola | 1939-04-07 | He was born in 1939 in Detr... | http://ia.media-imdb.com/im... |
| 3 | Quentin Tarantino | 1963-03-27 | In January of 1992, Reservo... | http://ia.media-imdb.com/im... |
| 4 | Sergio Leone | 1929-01-03 | Sergio Leone was virtually ... | http://ia.media-imdb.com/im... |
| 5 | Christopher Nolan | 1970-07-30 | Best known for his cerebral... | http://ia.media-imdb.com/im... |
| 6 | Sidney Lumet | 1924-06-25 | Sidney Lumet is a master of... | http://ia.media-imdb.com/im... |
| 7 | Steven Spielberg | 1946-12-18 | Undoubtedly one of the most... | http://ia.media-imdb.com/im... |
| 8 | Peter Jackson | 1961-10-31 | Peter Jackson was born as a... | http://ia.media-imdb.com/im... |
| id | name | dob | bio | image |
|---|---|---|---|---|
| 1 | Tim Robbins | 1958-10-16 | Born in West Covina, Califo... | http://ia.media-imdb.com/im... |
| 2 | Morgan Freeman | 1937-06-01 | With an authoritative voice... | http://ia.media-imdb.com/im... |
| 3 | Bob Gunton | 1945-11-15 | http://ia.media-imdb.com/im... | |
| 4 | William Sadler | 1950-04-13 | William Thomas Sadler was b... | http://ia.media-imdb.com/im... |
| 5 | Clancy Brown | 1959-01-05 | A tall, wavy-haired US acto... | http://ia.media-imdb.com/im... |
| 6 | Gil Bellows | 1967-06-28 | http://ia.media-imdb.com/im... | |
| 7 | Mark Rolston | 1956-12-07 | Mark knew he wanted to be a... | http://ia.media-imdb.com/im... |
| 8 | James Whitmore | 1921-10-01 | Born on October 1, 1921, in... | http://ia.media-imdb.com/im... |
| id | actor_id | movie_id | name |
|---|---|---|---|
| 1 | 1 | 1 | Andy Dufresne |
| 2 | 2 | 1 | Ellis Boyd 'Red' Redding |
| 3 | 3 | 1 | Warden Norton |
| 4 | 4 | 1 | Heywood |
| 5 | 5 | 1 | Captain Hadley |
| 6 | 6 | 1 | Tommy |
| 7 | 7 | 1 | Bogs Diamond |
| 8 | 8 | 1 | Brooks Hatlen |
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 })
=> #<Dish::ActiveRecord_Relation:0x00007fa4341e1ee8>