简体   繁体   English

Date.today减去18年Ruby

[英]Date.today minus 18 years Ruby

In my rails application the user can search for patients, and select how old the patient should maximal be! 在我的rails应用程序中,用户可以搜索患者,并选择患者应该最大的年龄!

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: 用户在例如15的年龄中键入最大值,因此我必须将输入转换为日期:

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) Patient.birthday => 29.09.1994 (意思是18岁)

and the user input for maximal => 18 并且用户输入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! 我知道我必须改变我的def年,但我不知道怎么做! Maybe you can help me Thanks! 也许你可以帮我谢谢!

UPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATEUPDATE 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'

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. 请注意,主要变化是指tomorrow而不是today ,并将年份再降低一年。 By doing that you've pushed the birthday down to the very last day of the 18th year. 通过这样做,你把生日推到了第18年的最后一天。

The year method now gives you all the people of that age. year方法现在为您提供该年龄段的所有人。 You just need to redefine the name of the method to something like min_birthdate_for_age( age ) . 您只需要将方法的名称重新定义为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, ie >= , change it to earlier dates, ie < . 但是现在如果您想使用此方法查找至少达到该年龄的人,请相应地更改搜索:而不是搜索当天或之后出生的人,即>= ,将其更改为更早的日期,即<

@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. @MattJohnson对一个类似的问题提出了很好的参考,这个问题也值得注意,所以请结合我的回答。 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

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: 它看起来像你试图若干年前获得的日期,在这种情况下,你应该使用yearsago Rails所提供的方法:

18.years.ago

Which, in context, would give you: 在上下文中,它会给你:

patients = patients.where("birthday >= ?", maximal.years.ago) if maximal.present?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM