-1

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'
John Smith
  • 6,105
  • 16
  • 58
  • 109
  • 3
    possible duplicate of [Get person's age in Ruby](http://stackoverflow.com/questions/819263/get-persons-age-in-ruby) – Matt Johnson-Pint Sep 14 '13 at 19:19
  • What does line 28 in `app/models/search.rb` look like? – lurker Sep 14 '13 at 20:21
  • 28 is dob = Date.new(dobss) Actually :birthday is defined as date in my model – John Smith Sep 14 '13 at 20:22
  • The problem is that i think that im writing #{age(:birthday)} But :birthday as symbol is wrong? The thing is when i write it like this #{age(birthday)} i get the error: undefined local variable or method `birthday' – John Smith Sep 14 '13 at 20:27

2 Answers2

1

Try your same code with:

def year(maximal)
  a = Date.tomorrow
  year = ( a.year - maximal ) - 1
  month = a.mon
  day = a.mday
  Date.new(year,month,day)
end

Notice the main change is to refer to tomorrow instead of today, and lower the year one more year. By doing that you've pushed the birthday down to the very last day of the 18th year.

The year method now gives you all the people of that age. You just need to redefine the name of the method to something like min_birthdate_for_age( age ). But now if you wanted to use this method to find people who are at least that age, then change the search accordingly: instead of searching for people of who were born on that day or after, i.e. >=, change it to earlier dates, i.e. <.

@MattJohnson made an excellent reference to a similar question which would be good to take note of as well, so use in combination with my answer. Basically he says that leap years will cause this kind of code to crash. A short workaround to avoid a leap year crash would be to re-write the method this way:

def min_birthdate_for_age( age )
  a = Date.tomorrow
  a.ago( ( age + 1 ).years )
end
Community
  • 1
  • 1
mjnissim
  • 3,102
  • 1
  • 19
  • 22
  • Very good, but you mean that when somebody has birthday the 31 of dezmber it wont work? – John Smith Sep 14 '13 at 19:58
  • Theres another problem, i would like to use the same function, as well for the minimum year? I dont thinks thats possible – John Smith Sep 14 '13 at 20:00
  • The good thing is that it should work with 31st of December as well. It might not work with some leap years, but you can test that and see. I might post a minimal year method when I get down to it. – mjnissim Sep 14 '13 at 20:22
  • Yes when you can post my an minimal yer def i would be very pleased ! Ant mark it as correct anwer – John Smith Sep 14 '13 at 20:30
1

It looks like you're trying to get the date a certain number of years ago, in which case you should use the years and ago methods that Rails provides:

18.years.ago

Which, in context, would give you:

patients = patients.where("birthday >= ?", maximal.years.ago) if maximal.present?
georgebrock
  • 28,393
  • 13
  • 77
  • 72