🕵️♀️ Task
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 |
|---|---|---|---|---|---|---|
| 33 | American History X | 1998 | 119 | A former neo-nazi skinhead ... | http://ia.media-imdb.com/im... | 25 |
| 34 | Apocalypse Now | 1979 | 153 | During the U.S.-Viet Nam Wa... | http://ia.media-imdb.com/im... | 2 |
| 35 | Terminator 2: Judgment Day | 1991 | 137 | The cyborg who once tried t... | http://ia.media-imdb.com/im... | 26 |
| 36 | Memento | 2000 | 113 | A man, suffering from short... | http://ia.media-imdb.com/im... | 5 |
| 37 | Saving Private Ryan | 1998 | 169 | Following the Normandy Land... | http://ia.media-imdb.com/im... | 7 |
| 38 | City Lights | 1931 | 87 | The Tramp struggles to help... | http://ia.media-imdb.com/im... | 27 |
| 39 | Dr. Strangelove or: How I L... | 1964 | 95 | An insane general starts a ... | http://ia.media-imdb.com/im... | 28 |
| 40 | Alien | 1979 | 117 | After investigating an S.O.... | http://ia.media-imdb.com/im... | 29 |
| id | name | dob | bio | image |
|---|---|---|---|---|
| 33 | Roman Polanski | 1933-08-18 | Roman Polanski is a Polish ... | http://ia.media-imdb.com/im... |
| 34 | Fritz Lang | 1890-12-05 | Fritz Lang was born in Vien... | http://ia.media-imdb.com/im... |
| id | name | dob | bio | image |
|---|---|---|---|---|
| 33 | Michael V. Gazzo | 1923-04-05 | Michael Vincente Gazzo was ... | http://ia.media-imdb.com/im... |
| 34 | G.D. Spradlin | 1920-08-31 | G.D. Spradlin started his c... | http://ia.media-imdb.com/im... |
| 35 | Richard Bright | 1937-06-28 | Fair complexioned, cold-eye... | |
| 36 | Gastone Moschin | 1929-06-08 | ||
| 37 | Tom Rosqui | 1928-06-12 | American character actor wi... | |
| 38 | Bruno Kirby | 1949-04-28 | Native New Yorker and Itali... | http://ia.media-imdb.com/im... |
| 39 | Frank Sivero | 1952-01-06 | http://ia.media-imdb.com/im... | |
| 40 | Francesca De Sapio | Born in Rome, Italy. After ... |
| id | actor_id | movie_id | name |
|---|---|---|---|
| 33 | 25 | 3 | Kay |
| 34 | 31 | 3 | Vito Corleone (as Robert De... |
| 35 | 29 | 3 | Fredo Corleone |
| 36 | 27 | 3 | Connie Corleone |
| 37 | 32 | 3 | Hyman Roth |
| 38 | 33 | 3 | Frankie Pentangeli |
| 39 | 34 | 3 | Senator Pat Geary |
| 40 | 35 | 3 | Al Neri |
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>