Rails scope filter by date range

人走茶凉 提交于 2019-12-06 12:49:29

问题


There are many questions relate to rails date range problem but mine is a little more complicated.

I have two models: house and booking. A House has_many bookings. A Booking has two attributes in date format: check_in and check_out.

What I want to achieve: Giving a valid date range, show all houses that are available during this range. In detail:

  • The start date of the range should not be in any booking.
  • The end date of the range should not be in any booking.
  • There should not be any booking between the start and the end.

Can this be done using the rails scope?

UPDATE:

I found the code below that can check scope date interval that overlaps.

named_scope :overlapping, lambda { |interval| {
:conditions => ["id <> ? AND (DATEDIFF(start_date, ?) * DATEDIFF(?, end_date)) >= 0", interval.id, interval.end_date, interval.start_date]
}}

How can I transfer this to my problem?


回答1:


scope :overlapping, (lambda do |start_date, end_date|
  House.includes(:bookings).where("bookings.check_in < ? AND bookings.check_out > ?", 
  start_date, end_date).references(:bookings).uniq
end)

I went ahead and deleted the >= and <= operators in favor of > and < to explicitly show these bookings being outside of the given range, but you can adjust them per your needs!

Update

Changed query to use #includes instead of #joins, since we're querying the attached table.




回答2:


Yes it is possible to have this query through scope. Put this scope in house model.

scope :overlapping, -> (start_date, end_date) { 
  includes(:bookings).where('bookings.check_in < ? AND bookings.check_out > ?',
  start_date.to_date, end_date.to_date)
}

And call as House.overlapping('2015-07-01', '2015-07-09')



来源:https://stackoverflow.com/questions/31013961/rails-scope-filter-by-date-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!