开发者

Rails and getting the amount of days dates overlap

开发者 https://www.devze.com 2023-01-14 14:08 出处:网络
lets say I have a user, this user has many trips and belongs to a city. Each trip belongs to a city and has a start and an end date.

lets say I have a user, this user has many trips and belongs to a city. Each trip belongs to a city and has a start and an end date.

Lets say user X goes to new york between 2010-09-01 and 2010-09-20, now I want to know who else is in new york and for how long their trips overlap with user x. And then there are people already l开发者_Python百科iving in New York so now I also want to know how many people that live in new york are currently there during any days that user x is there and for how many days they overlap. This is proving to be quite a challenge and I cant figure out the last step.

Some help would really be appreciated.

edit formatting


Assuming you have two models of type user each with a start_date and end_date. Assuming the two dates overlap, you can calculate the number of days overlapping with:

(user1.end_date - user2.start_date).to_i
0

精彩评论

暂无评论...
验证码 换一张
取 消