简体   繁体   中英

Rails and getting the amount of days dates overlap

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 living 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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