🕵️♀️ 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 |
|---|---|---|---|---|---|---|
| 25 | Casablanca | 1942 | 102 | Set in unoccupied Africa du... | http://ia.media-imdb.com/im... | 19 |
| 26 | The Usual Suspects | 1995 | 106 | A boat has been destroyed, ... | http://ia.media-imdb.com/im... | 20 |
| 27 | Raiders of the Lost Ark | 1981 | 115 | Archeologist and adventurer... | http://ia.media-imdb.com/im... | 7 |
| 28 | Rear Window | 1954 | 112 | A wheelchair bound photogra... | http://ia.media-imdb.com/im... | 21 |
| 29 | It's a Wonderful Life | 1946 | 130 | An angel helps a compassion... | http://ia.media-imdb.com/im... | 22 |
| 30 | Psycho | 1960 | 109 | A Phoenix secretary steals ... | http://ia.media-imdb.com/im... | 21 |
| 31 | Léon: The Professional | 1994 | 110 | A professional assassin res... | http://ia.media-imdb.com/im... | 23 |
| 32 | Sunset Blvd. | 1950 | 110 | A hack screenwriter writes ... | http://ia.media-imdb.com/im... | 24 |
| id | name | dob | bio | image |
|---|---|---|---|---|
| 25 | Tony Kaye | http://ia.media-imdb.com/im... | ||
| 26 | James Cameron | 1954-08-16 | James Francis Cameron was b... | http://ia.media-imdb.com/im... |
| 27 | Charles Chaplin | 1889-04-16 | Charlie Chaplin, considered... | http://ia.media-imdb.com/im... |
| 28 | Stanley Kubrick | 1928-07-26 | Stanley Kubrick was born in... | http://ia.media-imdb.com/im... |
| 29 | Ridley Scott | 1937-11-30 | Ridley Scott was born in So... | http://ia.media-imdb.com/im... |
| 30 | Hayao Miyazaki | 1941-01-05 | Hayao Miyazaki is one of Ja... | http://ia.media-imdb.com/im... |
| 31 | Orson Welles | 1915-05-06 | His father was a well-to-do... | http://ia.media-imdb.com/im... |
| 32 | Roberto Benigni | 1952-10-27 | http://ia.media-imdb.com/im... |
| id | name | dob | bio | image |
|---|---|---|---|---|
| 25 | Diane Keaton | 1946-01-05 | Diane Keaton was a Californ... | http://ia.media-imdb.com/im... |
| 26 | Abe Vigoda | 1921-02-24 | Tall, dour-faced and slouch... | http://ia.media-imdb.com/im... |
| 27 | Talia Shire | 1946-04-25 | http://ia.media-imdb.com/im... | |
| 28 | Gianni Russo | 1943-12-12 | With a singularly nebulous ... | http://ia.media-imdb.com/im... |
| 29 | John Cazale | 1935-08-12 | An Italian-American, Cazale... | http://ia.media-imdb.com/im... |
| 30 | Rudy Bond | 1912-10-01 | Rudolph Bond was raised in ... | |
| 31 | Robert De Niro | 1943-08-17 | Robert De Niro, who is thou... | http://ia.media-imdb.com/im... |
| 32 | Lee Strasberg | 1901-11-17 | Famed acting teacher Lee St... | http://ia.media-imdb.com/im... |
| id | actor_id | movie_id | name |
|---|---|---|---|
| 25 | 25 | 2 | Kay Adams |
| 26 | 26 | 2 | Tessio |
| 27 | 27 | 2 | Connie |
| 28 | 28 | 2 | Carlo |
| 29 | 29 | 2 | Fredo |
| 30 | 30 | 2 | Cuneo |
| 31 | 17 | 3 | Michael |
| 32 | 20 | 3 | Tom Hagen |
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>