In my rails application the user can search for patients, and select how old the patient should maximal be!
Here you can see my code, where only patients where the patient.birthday is minor or equal than the maximal age are selected.
patients = patients.where("birthday >= ?", year(maximal)) if maximal.present?
The User types in an age for eg 15 as maximal, so that i have to transform the input to an date:
def year(maximal)
a = Date.today
year = a.year - maximal
month = a.mon
day = a.mday
Date.new(year,month,day)
end
Now my problem: If i have for eg:
Patient.birthday => 29.09.1994 (means hes 18)
and the user input for maximal => 18
This user is not shown in the list although he should be displayed. I know i have to change my def year but i dont know how! Maybe you can help me Thanks!
UPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATE
Ok my new code:
patients = patients.where("#{age(:birthday)} >= ?", maximum) if maximum.present?
and
def age(dobss)
dob = Date.new(dobss)
now = Time.now.utc.to_date
now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
end
But now i get the error:
comparison of Symbol with 0 failed
app/models/search.rb:28:in `<'
app/models/search.rb:28:in `new'
app/models/search.rb:28:in `age'