mrg mrg - 2 months ago 9
Ruby Question

Retrive database records in Rails

My model named

Person
contains 3 columns namely
name
,
age
,
gender
.

Now how to get all the rows if the
gender = "male"
. I try to
fetch the data as shown below.

p = Person.find_by_gender("male")


The above statement properly worked. But it returns only 1 record. Because, the statement is converted to like following query.

SELECT "persons".* FROM "persons" WHERE "persons"."gender" = $1 LIMIT 1 [["gender", "male"]]


Due to limit is set to 1 it returns only 1 record. So, how to unset the limit? My requirement to get all the records in table if
gender

matches
"male"
.

Answer

use where

Person.where(gender: "male")

find method always returns only one record